home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / BNU22SR1.ZIP / src / binutils.2 / bfd / coff-alp.c < prev    next >
C/C++ Source or Header  |  1993-05-30  |  122KB  |  4,164 lines

  1. /* BFD back-end for Alpha Extended-Coff files.
  2.    Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
  3.  
  4.    Original version by Per Bothner
  5.    Full support added by Ian Lance Taylor, ian@cygnus.com.
  6.  
  7.    Modified from MIPS version by Steve Chamberlain (sac@cygnus.com)
  8.  
  9.    
  10. This file is part of BFD, the Binary File Descriptor library.
  11.  
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; either version 2 of the License, or
  15. (at your option) any later version.
  16.  
  17. This program is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. GNU General Public License for more details.
  21.  
  22. You should have received a copy of the GNU General Public License
  23. along with this program; if not, write to the Free Software
  24. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.   */
  25.  
  26.  
  27. /* FIXME!!!  WARNING!!  This is work in progress
  28.    
  29.    This file is an almost exact copy of the MIPS version.  Right
  30.    now it needs major hacking to get it to work with gdb.
  31.  
  32.    If common stuff is found, perhaps the function
  33.    ecoff_slurp_symbolic_info and friends should be split out...
  34.    In the mean time, it's #ed out.
  35.  
  36. */
  37.  
  38. #include "bfd.h"
  39. #include "sysdep.h"
  40. #include "libbfd.h"
  41. #include "seclet.h"
  42. #include "aout/ar.h"
  43. #include "aout/ranlib.h"
  44. #include "coff/alpha.h"
  45. #include "coff/internal.h"
  46. #include "coff/sym.h"
  47. #include "coff/symconst.h"
  48. #include "coff/ecoff-ext.h"
  49. #include "libcoff.h"
  50. #include "libecoff.h"
  51.  
  52. /* Each canonical asymbol really looks like this.  */
  53.  
  54. typedef struct ecoff_symbol_struct
  55. {
  56.   /* The actual symbol which the rest of BFD works with */
  57.   asymbol symbol;
  58.  
  59.   /* The fdr for this symbol.  */
  60.   FDR *fdr;
  61.  
  62.   /* true if this is a local symbol rather than an external one.  */
  63.   boolean local;
  64.  
  65.   /* A pointer to the unswapped hidden information for this symbol */
  66.   union
  67.     {
  68.       struct sym_ext *lnative;
  69.       struct ext_ext *enative;
  70.     }
  71.   native;
  72. } ecoff_symbol_type;
  73.  
  74. /* We take the address of the first element of a asymbol to ensure that the
  75.    macro is only ever applied to an asymbol.  */
  76. #define ecoffsymbol(asymbol) ((ecoff_symbol_type *) (&((asymbol)->the_bfd)))
  77.  
  78. /* The page boundary used to align sections in the executable file.  */
  79. #define ROUND_SIZE 0x10000
  80.  
  81. /* The linker needs a section to hold small common variables while
  82.    linking.  There is no convenient way to create it when the linker
  83.    needs it, so we always create one for each BFD.  We then avoid
  84.    writing it out.  */
  85. #define SCOMMON ".scommon"
  86.  
  87. /* Alpha ECOFF has COFF sections, but the debugging information is
  88.    stored in a completely different format.  This files uses the some
  89.    of the swapping routines from coffswap.h, and some of the generic
  90.    COFF routines in coffgen.c, but, unlike the real COFF targets, does
  91.    not use coffcode.h itself.  */
  92.  
  93. /* Prototypes for static functions.  */
  94.  
  95. static boolean ecoff_bad_format_hook PARAMS ((bfd *abfd, PTR filehdr));
  96. static asection *ecoff_make_section_hook PARAMS ((bfd *abfd, char *name));
  97. static boolean ecoff_new_section_hook PARAMS ((bfd *abfd, asection *section));
  98. static boolean ecoff_mkobject PARAMS ((bfd *abfd));
  99. static PTR ecoff_mkobject_hook PARAMS ((bfd *abfd, PTR filehdr, PTR aouthdr));
  100. static boolean ecoff_set_arch_mach_hook PARAMS ((bfd *abfd, PTR filehdr));
  101. static long ecoff_sec_to_styp_flags PARAMS ((CONST char *name,
  102.                          flagword flags));
  103. static flagword ecoff_styp_to_sec_flags PARAMS ((bfd *abfd, PTR hdr));
  104. static asymbol *ecoff_make_empty_symbol PARAMS ((bfd *abfd));
  105. static void ecoff_set_symbol_info PARAMS ((bfd *abfd, SYMR *ecoff_sym,
  106.                        asymbol *asym, int ext));
  107. static boolean ecoff_slurp_symbol_table PARAMS ((bfd *abfd));
  108. static unsigned int ecoff_get_symtab_upper_bound PARAMS ((bfd *abfd));
  109. static unsigned int ecoff_get_symtab PARAMS ((bfd *abfd,
  110.                           asymbol **alocation));
  111. static void ecoff_emit_aggregate PARAMS ((bfd *abfd, char *string,
  112.                       RNDXR *rndx, long isym,
  113.                       CONST char *which));
  114. static char *ecoff_type_to_string PARAMS ((bfd *abfd, union aux_ext *aux_ptr,
  115.                        int indx, int bigendian));
  116. static void ecoff_print_symbol PARAMS ((bfd *abfd, PTR filep,
  117.                     asymbol *symbol,
  118.                     bfd_print_symbol_type how));
  119. static void ecoff_swap_reloc_in PARAMS ((bfd *abfd, RELOC *ext,
  120.                      struct internal_reloc *intern));
  121. static unsigned int ecoff_swap_reloc_out PARAMS ((bfd *abfd, PTR src,
  122.                           PTR dst));
  123. static bfd_reloc_status_type ecoff_generic_reloc PARAMS ((bfd *abfd,
  124.                               arelent *reloc,
  125.                               asymbol *symbol,
  126.                               PTR data,
  127.                               asection *section,
  128.                               bfd *output_bfd));
  129. static bfd_reloc_status_type ecoff_refhi_reloc PARAMS ((bfd *abfd,
  130.                             arelent *reloc,
  131.                             asymbol *symbol,
  132.                             PTR data,
  133.                             asection *section,
  134.                             bfd *output_bfd));
  135. static bfd_reloc_status_type ecoff_reflo_reloc PARAMS ((bfd *abfd,
  136.                             arelent *reloc,
  137.                             asymbol *symbol,
  138.                             PTR data,
  139.                             asection *section,
  140.                             bfd *output_bfd));
  141. static bfd_reloc_status_type ecoff_gprel_reloc PARAMS ((bfd *abfd,
  142.                             arelent *reloc,
  143.                             asymbol *symbol,
  144.                             PTR data,
  145.                             asection *section,
  146.                             bfd *output_bfd));
  147. static boolean ecoff_slurp_reloc_table PARAMS ((bfd *abfd, asection *section,
  148.                         asymbol **symbols));
  149. static unsigned int ecoff_canonicalize_reloc PARAMS ((bfd *abfd,
  150.                               asection *section,
  151.                               arelent **relptr,
  152.                               asymbol **symbols));
  153. static CONST struct reloc_howto_struct *ecoff_bfd_reloc_type_lookup
  154.   PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
  155. static boolean ecoff_find_nearest_line PARAMS ((bfd *abfd,
  156.                         asection *section,
  157.                         asymbol **symbols,
  158.                         bfd_vma offset,
  159.                         CONST char **filename_ptr,
  160.                         CONST char **fnname_ptr,
  161.                         unsigned int *retline_ptr));
  162. static void ecoff_clear_output_flags PARAMS ((bfd *abfd));
  163. static boolean ecoff_rel PARAMS ((bfd *output_bfd, bfd_seclet_type *seclet,
  164.                   asection *output_section, PTR data,
  165.                   boolean relocateable));
  166. static boolean ecoff_dump_seclet PARAMS ((bfd *abfd, bfd_seclet_type *seclet,
  167.                       asection *section, PTR data,
  168.                       boolean relocateable));
  169. static long ecoff_add_string PARAMS ((bfd *output_bfd, FDR *fdr,
  170.                       CONST char *string, boolean external));
  171. static boolean ecoff_get_debug PARAMS ((bfd *output_bfd,
  172.                     bfd_seclet_type *seclet,
  173.                     asection *section,
  174.                     boolean relocateable));
  175. static boolean ecoff_bfd_seclet_link PARAMS ((bfd *abfd, PTR data,
  176.                           boolean relocateable));
  177. static boolean ecoff_set_arch_mach PARAMS ((bfd *abfd,
  178.                         enum bfd_architecture arch,
  179.                         unsigned long machine));
  180. static int ecoff_sizeof_headers PARAMS ((bfd *abfd, boolean reloc));
  181. static void ecoff_compute_section_file_positions PARAMS ((bfd *abfd));
  182. static boolean ecoff_set_section_contents PARAMS ((bfd *abfd,
  183.                            asection *section,
  184.                            PTR location,
  185.                            file_ptr offset,
  186.                            bfd_size_type count));
  187. static boolean ecoff_write_object_contents PARAMS ((bfd *abfd));
  188. static unsigned int ecoff_armap_hash PARAMS ((CONST char *s,
  189.                           unsigned int *rehash,
  190.                           unsigned int size,
  191.                           unsigned int hlog));
  192. static boolean ecoff_slurp_armap PARAMS ((bfd *abfd));
  193. static boolean ecoff_write_armap PARAMS ((bfd *abfd, unsigned int elength,
  194.                       struct orl *map,
  195.                       unsigned int orl_count,
  196.                       int stridx));
  197. static bfd_target *ecoff_archive_p PARAMS ((bfd *abfd));
  198.  
  199. /* Get the generic COFF swapping routines, except for the reloc,
  200.    symbol, and lineno ones.  Give them ecoff names.  */
  201. #define MIPSECOFF
  202. #define NO_COFF_RELOCS
  203. #define NO_COFF_SYMBOLS
  204. #define NO_COFF_LINENOS
  205. #define coff_swap_filehdr_in ecoff_swap_filehdr_in
  206. #define coff_swap_filehdr_out ecoff_swap_filehdr_out
  207. #define coff_swap_aouthdr_in ecoff_swap_aouthdr_in
  208. #define coff_swap_aouthdr_out ecoff_swap_aouthdr_out
  209. #define coff_swap_scnhdr_in ecoff_swap_scnhdr_in
  210. #define coff_swap_scnhdr_out ecoff_swap_scnhdr_out
  211. #include "coffswap.h"
  212.  
  213. /* This stuff is somewhat copied from coffcode.h.  */
  214.  
  215. static asection bfd_debug_section = { "*DEBUG*" };
  216.  
  217. /* See whether the magic number matches.  */
  218.  
  219. static boolean
  220. ecoff_bad_format_hook (abfd, filehdr)
  221.      bfd *abfd;
  222.      PTR filehdr;
  223. {
  224.   struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
  225.  
  226.   if (ECOFFBADMAG (*internal_f))
  227.     return false;
  228.  
  229.   return true;
  230. }
  231.  
  232. /* This is a hook needed by SCO COFF, but we have nothing to do.  */
  233.  
  234. static asection *
  235. ecoff_make_section_hook (abfd, name)
  236.      bfd *abfd;
  237.      char *name;
  238. {
  239.   return (asection *) NULL;
  240. }
  241.  
  242. /* Initialize a new section.  */
  243.  
  244. static boolean
  245. ecoff_new_section_hook (abfd, section)
  246.      bfd *abfd;
  247.      asection *section;
  248. {
  249.   section->alignment_power = abfd->xvec->align_power_min;
  250.  
  251.   if (strcmp (section->name, _TEXT) == 0)
  252.     section->flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
  253.   else if (strcmp (section->name, _DATA) == 0
  254.        || strcmp (section->name, _SDATA) == 0)
  255.     section->flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
  256.   else if (strcmp (section->name, _RDATA) == 0
  257.        || strcmp (section->name, _LIT8) == 0
  258.        || strcmp (section->name, _LIT4) == 0)
  259.     section->flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
  260.   else if (strcmp (section->name, _BSS) == 0
  261.        || strcmp (section->name, _SBSS) == 0)
  262.     section->flags |= SEC_ALLOC;
  263.  
  264.   /* Probably any other section name is SEC_NEVER_LOAD, but I'm
  265.      uncertain about .init on some systems and I don't know how shared
  266.      libraries work.  */
  267.  
  268.   return true;
  269. }
  270.  
  271. /* Set the alignment of a section; we have nothing to do.  */
  272.  
  273. #define ecoff_set_alignment_hook \
  274.   ((void (*) PARAMS ((bfd *, asection *, PTR))) bfd_void)
  275.  
  276. /* Create an ECOFF object.  */
  277.  
  278. static boolean
  279. ecoff_mkobject (abfd)
  280.      bfd *abfd;
  281. {
  282.   abfd->tdata.ecoff_obj_data = ((struct ecoff_tdata *)
  283.                 bfd_zalloc (abfd, sizeof (ecoff_data_type)));
  284.   if (abfd->tdata.ecoff_obj_data == NULL)
  285.     {
  286.       bfd_error = no_memory;
  287.       return false;
  288.     }
  289.  
  290.   /* Always create a .scommon section for every BFD.  This is a hack so
  291.      that the linker has something to attach scSCommon symbols to.  */
  292.   bfd_make_section (abfd, SCOMMON);
  293.  
  294.   return true;
  295. }
  296.  
  297. /* Create the ECOFF backend specific information.  */
  298.  
  299. static PTR
  300. ecoff_mkobject_hook (abfd, filehdr, aouthdr)
  301.      bfd *abfd;
  302.      PTR filehdr;
  303.      PTR aouthdr;
  304. {
  305.   struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
  306.   struct internal_aouthdr *internal_a = (struct internal_aouthdr *) aouthdr;
  307.   ecoff_data_type *ecoff;
  308.  
  309.   if (ecoff_mkobject (abfd) == false)
  310.     return NULL;
  311.  
  312.   ecoff = ecoff_data (abfd);
  313.   ecoff->gp_size = 8;
  314.   ecoff->sym_filepos = internal_f->f_symptr;
  315.  
  316.   if (internal_a != (struct internal_aouthdr *) NULL)
  317.     {
  318.       int i;
  319.  
  320.       ecoff->text_start = internal_a->text_start;
  321.       ecoff->text_end = internal_a->text_start + internal_a->tsize;
  322.       ecoff->gp = internal_a->gp_value;
  323.       ecoff->gprmask = internal_a->gprmask;
  324.       for (i = 0; i < 4; i++)
  325.     ecoff->cprmask[i] = internal_a->cprmask[i];
  326.     }
  327.  
  328.   return (PTR) ecoff;
  329. }
  330.  
  331. /* Determine the machine architecture and type.  */
  332.  
  333. static boolean
  334. ecoff_set_arch_mach_hook (abfd, filehdr)
  335.      bfd *abfd;
  336.      PTR filehdr;
  337. {
  338.   struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
  339.   enum bfd_architecture arch;
  340.  
  341.   switch (internal_f->f_magic)
  342.     {
  343.     case ALPHAMAGIC:
  344.       arch = bfd_arch_alpha;
  345.       break;
  346.  
  347.     default:
  348.       arch = bfd_arch_obscure;
  349.       break;
  350.     }
  351.  
  352.   bfd_default_set_arch_mach (abfd, arch, (unsigned long) 0);
  353.  
  354.   return true;
  355. }
  356.  
  357. /* Get the section s_flags to use for a section.  */
  358.  
  359. static long
  360. ecoff_sec_to_styp_flags (name, flags)
  361.      CONST char *name;
  362.      flagword flags;
  363. {
  364.   long styp;
  365.  
  366.   styp = 0;
  367.  
  368.   if (strcmp (name, _TEXT) == 0)
  369.     styp = STYP_TEXT;
  370.   else if (strcmp (name, _DATA) == 0)
  371.     styp = STYP_DATA;
  372.   else if (strcmp (name, _SDATA) == 0)
  373.     styp = STYP_SDATA;
  374.   else if (strcmp (name, _RDATA) == 0)
  375.     styp = STYP_RDATA;
  376.   else if (strcmp (name, _LIT8) == 0)
  377.     styp = STYP_LIT8;
  378.   else if (strcmp (name, _LIT4) == 0)
  379.     styp = STYP_LIT4;
  380.   else if (strcmp (name, _BSS) == 0)
  381.     styp = STYP_BSS;
  382.   else if (strcmp (name, _SBSS) == 0)
  383.     styp = STYP_SBSS;
  384.   else if (flags & SEC_CODE) 
  385.     styp = STYP_TEXT;
  386.   else if (flags & SEC_DATA) 
  387.     styp = STYP_DATA;
  388.   else if (flags & SEC_READONLY)
  389.     styp = STYP_RDATA;
  390.   else if (flags & SEC_LOAD)
  391.     styp = STYP_TEXT;
  392.   else
  393.     styp = STYP_BSS;
  394.  
  395.   if (flags & SEC_NEVER_LOAD)
  396.     styp |= STYP_NOLOAD;
  397.  
  398.   return styp;
  399. }
  400.  
  401. /* Get the BFD flags to use for a section.  */
  402.  
  403. static flagword
  404. ecoff_styp_to_sec_flags (abfd, hdr)
  405.      bfd *abfd;
  406.      PTR hdr;
  407. {
  408.   struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
  409.   long styp_flags = internal_s->s_flags;
  410.   flagword sec_flags=0;
  411.  
  412.   if (styp_flags & STYP_NOLOAD)
  413.     sec_flags |= SEC_NEVER_LOAD;
  414.  
  415.   /* For 386 COFF, at least, an unloadable text or data section is
  416.      actually a shared library section.  */
  417.   if (styp_flags & STYP_TEXT)
  418.     {
  419.       if (sec_flags & SEC_NEVER_LOAD)
  420.     sec_flags |= SEC_CODE | SEC_SHARED_LIBRARY;
  421.       else
  422.     sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
  423.     }
  424.   else if ((styp_flags & STYP_DATA)
  425.        || (styp_flags & STYP_RDATA)
  426.        || (styp_flags & STYP_SDATA))
  427.     {
  428.       if (sec_flags & SEC_NEVER_LOAD)
  429.     sec_flags |= SEC_DATA | SEC_SHARED_LIBRARY;
  430.       else
  431.     sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
  432.       if (styp_flags & STYP_RDATA)
  433.     sec_flags |= SEC_READONLY;
  434.     }
  435.   else if ((styp_flags & STYP_BSS)
  436.        || (styp_flags & STYP_SBSS))
  437.     {
  438.       sec_flags |= SEC_ALLOC;
  439.     }
  440.   else if (styp_flags & STYP_INFO) 
  441.     {
  442.       sec_flags |= SEC_NEVER_LOAD;
  443.     }
  444.   else if ((styp_flags & STYP_LIT8)
  445.        || (styp_flags & STYP_LIT4))
  446.     {
  447.       sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
  448.     }
  449.   else
  450.     {
  451.       sec_flags |= SEC_ALLOC | SEC_LOAD;
  452.     }
  453.  
  454.   return sec_flags;
  455. }
  456.  
  457. #if 0
  458.  
  459. /* Read in and swap the important symbolic information for an ECOFF
  460.    object file.  FIXME: This is called by gdb.  If there is ever
  461.    another ECOFF target, it should be moved into some sort of target
  462.    specific structure.  */
  463.  
  464. boolean
  465. ecoff_slurp_symbolic_info (abfd)
  466.      bfd *abfd;
  467. {
  468.   struct hdr_ext external_symhdr;
  469.   HDRR *internal_symhdr;
  470.   bfd_size_type raw_base;
  471.   bfd_size_type raw_size;
  472.   PTR raw;
  473.   struct fdr_ext *fraw_src;
  474.   struct fdr_ext *fraw_end;
  475.   struct fdr *fdr_ptr;
  476.  
  477.   /* Check whether we've already gotten it, and whether there's any to
  478.      get.  */
  479.   if (ecoff_data (abfd)->raw_syments != (PTR) NULL)
  480.     return true;
  481.   if (ecoff_data (abfd)->sym_filepos == 0)
  482.     {
  483.       bfd_get_symcount (abfd) = 0;
  484.       return true;
  485.     }
  486.  
  487.   /* At this point bfd_get_symcount (abfd) holds the number of symbols
  488.      as read from the file header, but on ECOFF this is always the
  489.      size of the symbolic information header.  It would be cleaner to
  490.      handle this when we first read the file in coffgen.c.  */
  491.   if (bfd_get_symcount (abfd) != sizeof (external_symhdr))
  492.     {
  493.       bfd_error = bad_value;
  494.       return false;
  495.     }
  496.  
  497.   /* Read the symbolic information header.  */
  498.   if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos, SEEK_SET) == -1
  499.       || (bfd_read ((PTR) &external_symhdr, sizeof (external_symhdr), 1, abfd)
  500.       != sizeof (external_symhdr)))
  501.     {
  502.       bfd_error = system_call_error;
  503.       return false;
  504.     }
  505.   internal_symhdr = &ecoff_data (abfd)->symbolic_header;
  506.   ecoff_swap_hdr_in (abfd, &external_symhdr, internal_symhdr);
  507.  
  508.   if (internal_symhdr->magic != magicSym)
  509.     {
  510.       bfd_error = bad_value;
  511.       return false;
  512.     }
  513.  
  514.   /* Now we can get the correct number of symbols.  */
  515.   bfd_get_symcount (abfd) = (internal_symhdr->isymMax
  516.                  + internal_symhdr->iextMax);
  517.  
  518.   /* Read all the symbolic information at once.  */
  519.   raw_base = ecoff_data (abfd)->sym_filepos + sizeof (struct hdr_ext);
  520.  
  521.   if (internal_symhdr->cbExtOffset != 0)
  522.     raw_size = (internal_symhdr->cbExtOffset
  523.         - raw_base
  524.         + internal_symhdr->iextMax * sizeof (struct ext_ext));
  525.   else
  526.     {
  527.       long cbline, issmax, issextmax;
  528.  
  529.       cbline = (internal_symhdr->cbLine + 3) &~ 3;
  530.       issmax = (internal_symhdr->issMax + 3) &~ 3;
  531.       issextmax = (internal_symhdr->issExtMax + 3) &~ 3;
  532.       raw_size = (cbline * sizeof (unsigned char)
  533.           + internal_symhdr->idnMax * sizeof (struct dnr_ext)
  534.           + internal_symhdr->ipdMax * sizeof (struct pdr_ext)
  535.           + internal_symhdr->isymMax * sizeof (struct sym_ext)
  536.           + internal_symhdr->ioptMax * sizeof (struct opt_ext)
  537.           + internal_symhdr->iauxMax * sizeof (union aux_ext)
  538.           + issmax * sizeof (char)
  539.           + issextmax * sizeof (char)
  540.           + internal_symhdr->ifdMax * sizeof (struct fdr_ext)
  541.           + internal_symhdr->crfd * sizeof (struct rfd_ext)
  542.           + internal_symhdr->iextMax * sizeof (struct ext_ext));
  543.     }
  544.  
  545.   if (raw_size == 0)
  546.     {
  547.       ecoff_data (abfd)->sym_filepos = 0;
  548.       return true;
  549.     }
  550.   raw = (PTR) bfd_alloc (abfd, raw_size);
  551.   if (raw == NULL)
  552.     {
  553.       bfd_error = no_memory;
  554.       return false;
  555.     }
  556.   if (bfd_read (raw, raw_size, 1, abfd) != raw_size)
  557.     {
  558.       bfd_error = system_call_error;
  559.       bfd_release (abfd, raw);
  560.       return false;
  561.     }
  562.  
  563.   ecoff_data (abfd)->raw_size = raw_size;
  564.   ecoff_data (abfd)->raw_syments = raw;
  565.  
  566.   /* Get pointers for the numeric offsets in the HDRR structure.  */
  567. #define FIX(off1, off2, type) \
  568.   if (internal_symhdr->off1 == 0) \
  569.     ecoff_data (abfd)->off2 = (type *) NULL; \
  570.   else \
  571.     ecoff_data (abfd)->off2 = (type *) ((char *) raw \
  572.                     + internal_symhdr->off1 \
  573.                     - raw_base)
  574.   FIX (cbLineOffset, line, unsigned char);
  575.   FIX (cbDnOffset, external_dnr, struct dnr_ext);
  576.   FIX (cbPdOffset, external_pdr, struct pdr_ext);
  577.   FIX (cbSymOffset, external_sym, struct sym_ext);
  578.   FIX (cbOptOffset, external_opt, struct opt_ext);
  579.   FIX (cbAuxOffset, external_aux, union aux_ext);
  580.   FIX (cbSsOffset, ss, char);
  581.   FIX (cbSsExtOffset, ssext, char);
  582.   FIX (cbFdOffset, external_fdr, struct fdr_ext);
  583.   FIX (cbRfdOffset, external_rfd, struct rfd_ext);
  584.   FIX (cbExtOffset, external_ext, struct ext_ext);
  585. #undef FIX
  586.  
  587.   /* I don't want to always swap all the data, because it will just
  588.      waste time and most programs will never look at it.  The only
  589.      time the linker needs most of the debugging information swapped
  590.      is when linking big-endian and little-endian MIPS object files
  591.      together, which is not a common occurrence.
  592.  
  593.      We need to look at the fdr to deal with a lot of information in
  594.      the symbols, so we swap them here.  */
  595.   ecoff_data (abfd)->fdr = (struct fdr *) bfd_alloc (abfd,
  596.                              (internal_symhdr->ifdMax *
  597.                               sizeof (struct fdr)));
  598.   if (ecoff_data (abfd)->fdr == NULL)
  599.     {
  600.       bfd_error = no_memory;
  601.       return false;
  602.     }
  603.   fdr_ptr = ecoff_data (abfd)->fdr;
  604.   fraw_src = ecoff_data (abfd)->external_fdr;
  605.   fraw_end = fraw_src + internal_symhdr->ifdMax;
  606.   for (; fraw_src < fraw_end; fraw_src++, fdr_ptr++)
  607.     ecoff_swap_fdr_in (abfd, fraw_src, fdr_ptr);
  608.  
  609.   return true;
  610. }
  611.  
  612. #endif
  613.  
  614. /* ECOFF symbol table routines.  The ECOFF symbol table is described
  615.    in gcc/mips-tfile.c.  */
  616.  
  617. /* ECOFF uses two common sections.  One is the usual one, and the
  618.    other is for small objects.  All the small objects are kept
  619.    together, and then referenced via the gp pointer, which yields
  620.    faster assembler code.  This is what we use for the small common
  621.    section.  */
  622. static asection ecoff_scom_section;
  623. static asymbol ecoff_scom_symbol;
  624. static asymbol *ecoff_scom_symbol_ptr;
  625.  
  626. /* Create an empty symbol.  */
  627.  
  628. static asymbol *
  629. ecoff_make_empty_symbol (abfd)
  630.      bfd *abfd;
  631. {
  632.   ecoff_symbol_type *new;
  633.  
  634.   new = (ecoff_symbol_type *) bfd_alloc (abfd, sizeof (ecoff_symbol_type));
  635.   if (new == (ecoff_symbol_type *) NULL)
  636.     {
  637.       bfd_error = no_memory;
  638.       return (asymbol *) NULL;
  639.     }
  640.   memset (new, 0, sizeof *new);
  641.   new->symbol.section = (asection *) NULL;
  642.   new->fdr = (FDR *) NULL;
  643.   new->local = false;
  644.   new->native.lnative = (struct sym_ext *) NULL;
  645.   new->symbol.the_bfd = abfd;
  646.   return &new->symbol;
  647. }
  648.  
  649. /* Set the BFD flags and section for an ECOFF symbol.  */
  650.  
  651. static void
  652. ecoff_set_symbol_info (abfd, ecoff_sym, asym, ext)
  653.      bfd *abfd;
  654.      SYMR *ecoff_sym;
  655.      asymbol *asym;
  656.      int ext;
  657. {
  658.   asym->the_bfd = abfd;
  659.   asym->value = ecoff_sym->value;
  660.   asym->section = &bfd_debug_section;
  661.   asym->udata = NULL;
  662.  
  663.   /* Most symbol types are just for debugging.  */
  664.   switch (ecoff_sym->st)
  665.     {
  666.     case stGlobal:
  667.     case stStatic:
  668.     case stLabel:
  669.     case stProc:
  670.     case stStaticProc:
  671.       break;
  672.     case stNil:
  673.       if (MIPS_IS_STAB (ecoff_sym))
  674.     {
  675.       asym->flags = BSF_DEBUGGING;
  676.       return;
  677.     }
  678.       break;
  679.     default:
  680.       asym->flags = BSF_DEBUGGING;
  681.       return;
  682.     }
  683.  
  684.   if (ext)
  685.     asym->flags = BSF_EXPORT | BSF_GLOBAL;
  686.   else
  687.     asym->flags = BSF_LOCAL;
  688.   switch (ecoff_sym->sc)
  689.     {
  690.     case scNil:
  691.       /* Used for compiler generated labels.  Leave them in the
  692.      debugging section, and mark them as local.  If BSF_DEBUGGING
  693.      is set, then nm does not display them for some reason.  If no
  694.      flags are set then the linker whines about them.  */
  695.       asym->flags = BSF_LOCAL;
  696.       break;
  697.     case scText:
  698.       asym->section = bfd_make_section_old_way (abfd, ".text");
  699.       asym->value -= asym->section->vma;
  700.       break;
  701.     case scData:
  702.       asym->section = bfd_make_section_old_way (abfd, ".data");
  703.       asym->value -= asym->section->vma;
  704.       break;
  705.     case scBss:
  706.       if (ext)
  707.     {
  708.       asym->section = &bfd_com_section;
  709.       asym->flags = 0;
  710.     }
  711.       else
  712.     {
  713.       asym->section = bfd_make_section_old_way (abfd, ".bss");
  714.       asym->value -= asym->section->vma;
  715.     }
  716.       break;
  717.     case scRegister:
  718.       asym->flags = BSF_DEBUGGING;
  719.       break;
  720.     case scAbs:
  721.       asym->section = &bfd_abs_section;
  722.       break;
  723.     case scUndefined:
  724.       asym->section = &bfd_und_section;
  725.       asym->flags = 0;
  726.       asym->value = 0;
  727.       break;
  728.     case scCdbLocal:
  729.     case scBits:
  730.     case scCdbSystem:
  731.     case scRegImage:
  732.     case scInfo:
  733.     case scUserStruct:
  734.       asym->flags = BSF_DEBUGGING;
  735.       break;
  736.     case scSData:
  737.       asym->section = bfd_make_section_old_way (abfd, ".sdata");
  738.       asym->value -= asym->section->vma;
  739.       break;
  740.     case scSBss:
  741.       asym->section = bfd_make_section_old_way (abfd, ".sbss");
  742.       if (! ext)
  743.     asym->value -= asym->section->vma;
  744.       break;
  745.     case scRData:
  746.       asym->section = bfd_make_section_old_way (abfd, ".rdata");
  747.       asym->value -= asym->section->vma;
  748.       break;
  749.     case scVar:
  750.       asym->flags = BSF_DEBUGGING;
  751.       break;
  752.     case scCommon:
  753.       if (asym->value > ecoff_data (abfd)->gp_size)
  754.     {
  755.       asym->section = &bfd_com_section;
  756.       asym->flags = 0;
  757.       break;
  758.     }
  759.       /* Fall through.  */
  760.     case scSCommon:
  761.       if (ecoff_scom_section.name == NULL)
  762.     {
  763.       /* Initialize the small common section.  */
  764.       ecoff_scom_section.name = SCOMMON;
  765.       ecoff_scom_section.flags = SEC_IS_COMMON;
  766.       ecoff_scom_section.output_section = &ecoff_scom_section;
  767.       ecoff_scom_section.symbol = &ecoff_scom_symbol;
  768.       ecoff_scom_section.symbol_ptr_ptr = &ecoff_scom_symbol_ptr;
  769.       ecoff_scom_symbol.name = SCOMMON;
  770.       ecoff_scom_symbol.flags = BSF_SECTION_SYM;
  771.       ecoff_scom_symbol.section = &ecoff_scom_section;
  772.       ecoff_scom_symbol_ptr = &ecoff_scom_symbol;
  773.     }
  774.       asym->section = &ecoff_scom_section;
  775.       asym->flags = 0;
  776.       break;
  777.     case scVarRegister:
  778.     case scVariant:
  779.       asym->flags = BSF_DEBUGGING;
  780.       break;
  781.     case scSUndefined:
  782.       asym->section = &bfd_und_section;
  783.       asym->flags = 0;
  784.       asym->value = 0;
  785.       break;
  786.     case scInit:
  787.       asym->section = bfd_make_section_old_way (abfd, ".init");
  788.       asym->value -= asym->section->vma;
  789.       break;
  790.     case scBasedVar:
  791.     case scXData:
  792.     case scPData:
  793.       asym->flags = BSF_DEBUGGING;
  794.       break;
  795.     case scFini:
  796.       asym->section = bfd_make_section_old_way (abfd, ".fini");
  797.       asym->value -= asym->section->vma;
  798.       break;
  799.     default:
  800.       break;
  801.     }
  802. }
  803.  
  804. /* Read an ECOFF symbol table.  */
  805.  
  806. static boolean
  807. ecoff_slurp_symbol_table (abfd)
  808.      bfd *abfd;
  809. {
  810.   bfd_size_type internal_size;
  811.   ecoff_symbol_type *internal;
  812.   ecoff_symbol_type *internal_ptr;
  813.   struct ext_ext *eraw_src;
  814.   struct ext_ext *eraw_end;
  815.   FDR *fdr_ptr;
  816.   FDR *fdr_end;
  817.  
  818.   /* If we've already read in the symbol table, do nothing.  */
  819.   if (ecoff_data (abfd)->canonical_symbols != NULL)
  820.     return true;
  821.  
  822.   /* Get the symbolic information.  */
  823.   if (ecoff_slurp_symbolic_info (abfd) == false)
  824.     return false;
  825.   if (bfd_get_symcount (abfd) == 0)
  826.     return true;
  827.  
  828.   internal_size = bfd_get_symcount (abfd) * sizeof (ecoff_symbol_type);
  829.   internal = (ecoff_symbol_type *) bfd_alloc (abfd, internal_size);
  830.   if (internal == NULL)
  831.     {
  832.       bfd_error = no_memory;
  833.       return false;
  834.     }
  835.  
  836.   internal_ptr = internal;
  837.   eraw_src = ecoff_data (abfd)->external_ext;
  838.   eraw_end = eraw_src + ecoff_data (abfd)->symbolic_header.iextMax;
  839.   for (; eraw_src < eraw_end; eraw_src++, internal_ptr++)
  840.     {
  841.       EXTR internal_esym;
  842.  
  843.       ecoff_swap_ext_in (abfd, eraw_src, &internal_esym);
  844.       internal_ptr->symbol.name = (ecoff_data (abfd)->ssext
  845.                    + internal_esym.asym.iss);
  846.       ecoff_set_symbol_info (abfd, &internal_esym.asym,
  847.                  &internal_ptr->symbol, 1);
  848.       internal_ptr->fdr = ecoff_data (abfd)->fdr + internal_esym.ifd;
  849.       internal_ptr->local = false;
  850.       internal_ptr->native.enative = eraw_src;
  851.     }
  852.  
  853.   /* The local symbols must be accessed via the fdr's, because the
  854.      string and aux indices are relative to the fdr information.  */
  855.   fdr_ptr = ecoff_data (abfd)->fdr;
  856.   fdr_end = fdr_ptr + ecoff_data (abfd)->symbolic_header.ifdMax;
  857.   for (; fdr_ptr < fdr_end; fdr_ptr++)
  858.     {
  859.       struct sym_ext *lraw_src;
  860.       struct sym_ext *lraw_end;
  861.  
  862.       lraw_src = ecoff_data (abfd)->external_sym + fdr_ptr->isymBase;
  863.       lraw_end = lraw_src + fdr_ptr->csym;
  864.       for (; lraw_src < lraw_end; lraw_src++, internal_ptr++)
  865.     {
  866.       SYMR internal_sym;
  867.  
  868.       ecoff_swap_sym_in (abfd, lraw_src, &internal_sym);
  869.       internal_ptr->symbol.name = (ecoff_data (abfd)->ss
  870.                        + fdr_ptr->issBase
  871.                        + internal_sym.iss);
  872.       ecoff_set_symbol_info (abfd, &internal_sym,
  873.                  &internal_ptr->symbol, 0);
  874.       internal_ptr->fdr = fdr_ptr;
  875.       internal_ptr->local = true;
  876.       internal_ptr->native.lnative = lraw_src;
  877.     }
  878.     }
  879.  
  880.   ecoff_data (abfd)->canonical_symbols = internal;
  881.  
  882.   return true;
  883. }
  884.  
  885. static unsigned int
  886. ecoff_get_symtab_upper_bound (abfd)
  887.      bfd *abfd;
  888. {
  889.   if (ecoff_slurp_symbolic_info (abfd) == false
  890.       || bfd_get_symcount (abfd) == 0)
  891.     return 0;
  892.  
  893.   return (bfd_get_symcount (abfd) + 1) * (sizeof (ecoff_symbol_type *));
  894. }
  895.  
  896. static unsigned int
  897. ecoff_get_symtab (abfd, alocation)
  898.      bfd *abfd;
  899.      asymbol **alocation;
  900. {
  901.   unsigned int counter = 0;
  902.   ecoff_symbol_type *symbase;
  903.   ecoff_symbol_type **location = (ecoff_symbol_type **) alocation;
  904.  
  905.   if (ecoff_slurp_symbol_table (abfd) == false
  906.       || bfd_get_symcount (abfd) == 0)
  907.     return 0;
  908.  
  909.   symbase = ecoff_data (abfd)->canonical_symbols;
  910.   while (counter < bfd_get_symcount (abfd))
  911.     {
  912.       *(location++) = symbase++;
  913.       counter++;
  914.     }
  915.   *location++ = (ecoff_symbol_type *) NULL;
  916.   return bfd_get_symcount (abfd);
  917. }
  918.  
  919. /* Turn ECOFF type information into a printable string.
  920.    ecoff_emit_aggregate and ecoff_type_to_string are from
  921.    gcc/mips-tdump.c, with swapping added and used_ptr removed.  */
  922.  
  923. /* Write aggregate information to a string.  */
  924.  
  925. static void
  926. ecoff_emit_aggregate (abfd, string, rndx, isym, which)
  927.      bfd *abfd;
  928.      char *string;
  929.      RNDXR *rndx;
  930.      long isym;
  931.      CONST char *which;
  932. {
  933.   int ifd = rndx->rfd;
  934.   int indx = rndx->index;
  935.   int sym_base, ss_base;
  936.   CONST char *name;
  937.   
  938.   if (ifd == 0xfff)
  939.     ifd = isym;
  940.  
  941.   sym_base = ecoff_data (abfd)->fdr[ifd].isymBase;
  942.   ss_base  = ecoff_data (abfd)->fdr[ifd].issBase;
  943.   
  944.   if (indx == indexNil)
  945.     name = "/* no name */";
  946.   else
  947.     {
  948.       SYMR sym;
  949.  
  950.       indx += sym_base;
  951.       ecoff_swap_sym_in (abfd,
  952.              ecoff_data (abfd)->external_sym + indx,
  953.              &sym);
  954.       name = ecoff_data (abfd)->ss + ss_base + sym.iss;
  955.     }
  956.  
  957.   sprintf (string,
  958.        "%s %s { ifd = %d, index = %d }",
  959.        which, name, ifd,
  960.        indx + ecoff_data (abfd)->symbolic_header.iextMax);
  961. }
  962.  
  963. /* Convert the type information to string format.  */
  964.  
  965. static char *
  966. ecoff_type_to_string (abfd, aux_ptr, indx, bigendian)
  967.      bfd *abfd;
  968.      union aux_ext *aux_ptr;
  969.      int indx;
  970.      int bigendian;
  971. {
  972.   AUXU u;
  973.   struct qual {
  974.     unsigned int  type;
  975.     int  low_bound;
  976.     int  high_bound;
  977.     int  stride;
  978.   } qualifiers[7];
  979.  
  980.   unsigned int basic_type;
  981.   int i;
  982.   static char buffer1[1024];
  983.   static char buffer2[1024];
  984.   char *p1 = buffer1;
  985.   char *p2 = buffer2;
  986.   RNDXR rndx;
  987.  
  988.   for (i = 0; i < 7; i++)
  989.     {
  990.       qualifiers[i].low_bound = 0;
  991.       qualifiers[i].high_bound = 0;
  992.       qualifiers[i].stride = 0;
  993.     }
  994.  
  995.   if (AUX_GET_ISYM (bigendian, &aux_ptr[indx]) == -1)
  996.     return "-1 (no type)";
  997.   ecoff_swap_tir_in (bigendian, &aux_ptr[indx++].a_ti, &u.ti);
  998.  
  999.   basic_type = u.ti.bt;
  1000.   qualifiers[0].type = u.ti.tq0;
  1001.   qualifiers[1].type = u.ti.tq1;
  1002.   qualifiers[2].type = u.ti.tq2;
  1003.   qualifiers[3].type = u.ti.tq3;
  1004.   qualifiers[4].type = u.ti.tq4;
  1005.   qualifiers[5].type = u.ti.tq5;
  1006.   qualifiers[6].type = tqNil;
  1007.  
  1008.   /*
  1009.    * Go get the basic type.
  1010.    */
  1011.   switch (basic_type)
  1012.     {
  1013.     case btNil:            /* undefined */
  1014.       strcpy (p1, "nil");
  1015.       break;
  1016.  
  1017.     case btAdr:            /* address - integer same size as pointer */
  1018.       strcpy (p1, "address");
  1019.       break;
  1020.  
  1021.     case btChar:        /* character */
  1022.       strcpy (p1, "char");
  1023.       break;
  1024.  
  1025.     case btUChar:        /* unsigned character */
  1026.       strcpy (p1, "unsigned char");
  1027.       break;
  1028.  
  1029.     case btShort:        /* short */
  1030.       strcpy (p1, "short");
  1031.       break;
  1032.  
  1033.     case btUShort:        /* unsigned short */
  1034.       strcpy (p1, "unsigned short");
  1035.       break;
  1036.  
  1037.     case btInt:            /* int */
  1038.       strcpy (p1, "int");
  1039.       break;
  1040.  
  1041.     case btUInt:        /* unsigned int */
  1042.       strcpy (p1, "unsigned int");
  1043.       break;
  1044.  
  1045.     case btLong:        /* long */
  1046.       strcpy (p1, "long");
  1047.       break;
  1048.  
  1049.     case btULong:        /* unsigned long */
  1050.       strcpy (p1, "unsigned long");
  1051.       break;
  1052.  
  1053.     case btFloat:        /* float (real) */
  1054.       strcpy (p1, "float");
  1055.       break;
  1056.  
  1057.     case btDouble:        /* Double (real) */
  1058.       strcpy (p1, "double");
  1059.       break;
  1060.  
  1061.       /* Structures add 1-2 aux words:
  1062.      1st word is [ST_RFDESCAPE, offset] pointer to struct def;
  1063.      2nd word is file index if 1st word rfd is ST_RFDESCAPE.  */
  1064.  
  1065.     case btStruct:        /* Structure (Record) */
  1066.       ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
  1067.       ecoff_emit_aggregate (abfd, p1, &rndx,
  1068.                 AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
  1069.                 "struct");
  1070.       indx++;            /* skip aux words */
  1071.       break;
  1072.  
  1073.       /* Unions add 1-2 aux words:
  1074.      1st word is [ST_RFDESCAPE, offset] pointer to union def;
  1075.      2nd word is file index if 1st word rfd is ST_RFDESCAPE.  */
  1076.  
  1077.     case btUnion:        /* Union */
  1078.       ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
  1079.       ecoff_emit_aggregate (abfd, p1, &rndx,
  1080.                 AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
  1081.                 "union");
  1082.       indx++;            /* skip aux words */
  1083.       break;
  1084.  
  1085.       /* Enumerations add 1-2 aux words:
  1086.      1st word is [ST_RFDESCAPE, offset] pointer to enum def;
  1087.      2nd word is file index if 1st word rfd is ST_RFDESCAPE.  */
  1088.  
  1089.     case btEnum:        /* Enumeration */
  1090.       ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
  1091.       ecoff_emit_aggregate (abfd, p1, &rndx,
  1092.                 AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
  1093.                 "enum");
  1094.       indx++;            /* skip aux words */
  1095.       break;
  1096.  
  1097.     case btTypedef:        /* defined via a typedef, isymRef points */
  1098.       strcpy (p1, "typedef");
  1099.       break;
  1100.  
  1101.     case btRange:        /* subrange of int */
  1102.       strcpy (p1, "subrange");
  1103.       break;
  1104.  
  1105.     case btSet:            /* pascal sets */
  1106.       strcpy (p1, "set");
  1107.       break;
  1108.  
  1109.     case btComplex:        /* fortran complex */
  1110.       strcpy (p1, "complex");
  1111.       break;
  1112.  
  1113.     case btDComplex:        /* fortran double complex */
  1114.       strcpy (p1, "double complex");
  1115.       break;
  1116.  
  1117.     case btIndirect:        /* forward or unnamed typedef */
  1118.       strcpy (p1, "forward/unamed typedef");
  1119.       break;
  1120.  
  1121.     case btFixedDec:        /* Fixed Decimal */
  1122.       strcpy (p1, "fixed decimal");
  1123.       break;
  1124.  
  1125.     case btFloatDec:        /* Float Decimal */
  1126.       strcpy (p1, "float decimal");
  1127.       break;
  1128.  
  1129.     case btString:        /* Varying Length Character String */
  1130.       strcpy (p1, "string");
  1131.       break;
  1132.  
  1133.     case btBit:            /* Aligned Bit String */
  1134.       strcpy (p1, "bit");
  1135.       break;
  1136.  
  1137.     case btPicture:        /* Picture */
  1138.       strcpy (p1, "picture");
  1139.       break;
  1140.  
  1141.     case btVoid:        /* Void */
  1142.       strcpy (p1, "void");
  1143.       break;
  1144.  
  1145.     default:
  1146.       sprintf (p1, "Unknown basic type %d", (int) basic_type);
  1147.       break;
  1148.     }
  1149.  
  1150.   p1 += strlen (buffer1);
  1151.  
  1152.   /*
  1153.    * If this is a bitfield, get the bitsize.
  1154.    */
  1155.   if (u.ti.fBitfield)
  1156.     {
  1157.       int bitsize;
  1158.  
  1159.       bitsize = AUX_GET_WIDTH (bigendian, &aux_ptr[indx++]);
  1160.       sprintf (p1, " : %d", bitsize);
  1161.       p1 += strlen (buffer1);
  1162.     }
  1163.  
  1164.  
  1165.   /*
  1166.    * Deal with any qualifiers.
  1167.    */
  1168.   if (qualifiers[0].type != tqNil)
  1169.     {
  1170.       /*
  1171.        * Snarf up any array bounds in the correct order.  Arrays
  1172.        * store 5 successive words in the aux. table:
  1173.        *    word 0    RNDXR to type of the bounds (ie, int)
  1174.        *    word 1    Current file descriptor index
  1175.        *    word 2    low bound
  1176.        *    word 3    high bound (or -1 if [])
  1177.        *    word 4    stride size in bits
  1178.        */
  1179.       for (i = 0; i < 7; i++)
  1180.     {
  1181.       if (qualifiers[i].type == tqArray)
  1182.         {
  1183.           qualifiers[i].low_bound =
  1184.         AUX_GET_DNLOW (bigendian, &aux_ptr[indx+2]);
  1185.           qualifiers[i].high_bound =
  1186.         AUX_GET_DNHIGH (bigendian, &aux_ptr[indx+3]);
  1187.           qualifiers[i].stride =
  1188.         AUX_GET_WIDTH (bigendian, &aux_ptr[indx+4]);
  1189.           indx += 5;
  1190.         }
  1191.     }
  1192.  
  1193.       /*
  1194.        * Now print out the qualifiers.
  1195.        */
  1196.       for (i = 0; i < 6; i++)
  1197.     {
  1198.       switch (qualifiers[i].type)
  1199.         {
  1200.         case tqNil:
  1201.         case tqMax:
  1202.           break;
  1203.  
  1204.         case tqPtr:
  1205.           strcpy (p2, "ptr to ");
  1206.           p2 += sizeof ("ptr to ")-1;
  1207.           break;
  1208.  
  1209.         case tqVol:
  1210.           strcpy (p2, "volatile ");
  1211.           p2 += sizeof ("volatile ")-1;
  1212.           break;
  1213.  
  1214.         case tqFar:
  1215.           strcpy (p2, "far ");
  1216.           p2 += sizeof ("far ")-1;
  1217.           break;
  1218.  
  1219.         case tqProc:
  1220.           strcpy (p2, "func. ret. ");
  1221.           p2 += sizeof ("func. ret. ");
  1222.           break;
  1223.  
  1224.         case tqArray:
  1225.           {
  1226.         int first_array = i;
  1227.         int j;
  1228.  
  1229.         /* Print array bounds reversed (ie, in the order the C
  1230.            programmer writes them).  C is such a fun language.... */
  1231.  
  1232.         while (i < 5 && qualifiers[i+1].type == tqArray)
  1233.           i++;
  1234.  
  1235.         for (j = i; j >= first_array; j--)
  1236.           {
  1237.             strcpy (p2, "array [");
  1238.             p2 += sizeof ("array [")-1;
  1239.             if (qualifiers[j].low_bound != 0)
  1240.               sprintf (p2,
  1241.                    "%ld:%ld {%ld bits}",
  1242.                    (long) qualifiers[j].low_bound,
  1243.                    (long) qualifiers[j].high_bound,
  1244.                    (long) qualifiers[j].stride);
  1245.  
  1246.             else if (qualifiers[j].high_bound != -1)
  1247.               sprintf (p2,
  1248.                    "%ld {%ld bits}",
  1249.                    (long) (qualifiers[j].high_bound + 1),
  1250.                    (long) (qualifiers[j].stride));
  1251.  
  1252.             else
  1253.               sprintf (p2, " {%ld bits}", (long) (qualifiers[j].stride));
  1254.  
  1255.             p2 += strlen (p2);
  1256.             strcpy (p2, "] of ");
  1257.             p2 += sizeof ("] of ")-1;
  1258.           }
  1259.           }
  1260.           break;
  1261.         }
  1262.     }
  1263.     }
  1264.  
  1265.   strcpy (p2, buffer1);
  1266.   return buffer2;
  1267. }
  1268.  
  1269. /* Print information about an ECOFF symbol.  */
  1270.  
  1271. static void
  1272. ecoff_print_symbol (abfd, filep, symbol, how)
  1273.      bfd *abfd;
  1274.      PTR filep;
  1275.      asymbol *symbol;
  1276.      bfd_print_symbol_type how;
  1277. {
  1278.   FILE *file = (FILE *)filep;
  1279.  
  1280.   switch (how)
  1281.     {
  1282.     case bfd_print_symbol_name:
  1283.       fprintf (file, "%s", symbol->name);
  1284.       break;
  1285.     case bfd_print_symbol_more:
  1286.       if (ecoffsymbol (symbol)->local)
  1287.     {
  1288.       SYMR ecoff_sym;
  1289.     
  1290.       ecoff_swap_sym_in (abfd, ecoffsymbol (symbol)->native.lnative,
  1291.                  &ecoff_sym);
  1292.       fprintf (file, "ecoff local %lx %x %x",
  1293.            (unsigned long) ecoff_sym.value,
  1294.            (unsigned) ecoff_sym.st, (unsigned) ecoff_sym.sc);
  1295.     }
  1296.       else
  1297.     {
  1298.       EXTR ecoff_ext;
  1299.  
  1300.       ecoff_swap_ext_in (abfd, ecoffsymbol (symbol)->native.enative,
  1301.                  &ecoff_ext);
  1302.       fprintf (file, "ecoff extern %lx %x %x",
  1303.            (unsigned long) ecoff_ext.asym.value,
  1304.            (unsigned) ecoff_ext.asym.st,
  1305.            (unsigned) ecoff_ext.asym.sc);
  1306.     }
  1307.       break;
  1308.     case bfd_print_symbol_nm:
  1309.       {
  1310.     CONST char *section_name = symbol->section->name;
  1311.  
  1312.     bfd_print_symbol_vandf ((PTR) file, symbol);
  1313.     fprintf (file, " %-5s %s %s",
  1314.          section_name,
  1315.          ecoffsymbol (symbol)->local ? "l" : "e",
  1316.          symbol->name);
  1317.       }
  1318.       break;
  1319.     case bfd_print_symbol_all:
  1320.       /* Print out the symbols in a reasonable way */
  1321.       {
  1322.     char type;
  1323.     int pos;
  1324.     EXTR ecoff_ext;
  1325.     char jmptbl;
  1326.     char cobol_main;
  1327.     char weakext;
  1328.  
  1329.     if (ecoffsymbol (symbol)->local)
  1330.       {
  1331.         ecoff_swap_sym_in (abfd, ecoffsymbol (symbol)->native.lnative,
  1332.                    &ecoff_ext.asym);
  1333.         type = 'l';
  1334.         pos = (ecoffsymbol (symbol)->native.lnative
  1335.            - ecoff_data (abfd)->external_sym
  1336.            + ecoff_data (abfd)->symbolic_header.iextMax);
  1337.         jmptbl = ' ';
  1338.         cobol_main = ' ';
  1339.         weakext = ' ';
  1340.       }
  1341.     else
  1342.       {
  1343.         ecoff_swap_ext_in (abfd, ecoffsymbol (symbol)->native.enative,
  1344.                    &ecoff_ext);
  1345.         type = 'e';
  1346.         pos = (ecoffsymbol (symbol)->native.enative
  1347.            - ecoff_data (abfd)->external_ext);
  1348.         jmptbl = ecoff_ext.jmptbl ? 'j' : ' ';
  1349.         cobol_main = ecoff_ext.cobol_main ? 'c' : ' ';
  1350.         weakext = ecoff_ext.weakext ? 'w' : ' ';
  1351.       }
  1352.  
  1353.     fprintf (file, "[%3d] %c %lx st %x sc %x indx %x %c%c%c %s",
  1354.          pos, type, (unsigned long) ecoff_ext.asym.value,
  1355.          (unsigned) ecoff_ext.asym.st,
  1356.          (unsigned) ecoff_ext.asym.sc,
  1357.          (unsigned) ecoff_ext.asym.index,
  1358.          jmptbl, cobol_main, weakext,
  1359.          symbol->name);
  1360.  
  1361.     if (ecoffsymbol (symbol)->fdr != NULL
  1362.         && ecoff_ext.asym.index != indexNil)
  1363.       {
  1364.         unsigned indx;
  1365.         int bigendian;
  1366.         long sym_base;
  1367.         union aux_ext *aux_base;
  1368.  
  1369.         indx = ecoff_ext.asym.index;
  1370.  
  1371.         /* sym_base is used to map the fdr relative indices which
  1372.            appear in the file to the position number which we are
  1373.            using.  */
  1374.         sym_base = ecoffsymbol (symbol)->fdr->isymBase;
  1375.         if (ecoffsymbol (symbol)->local)
  1376.           sym_base += ecoff_data (abfd)->symbolic_header.iextMax;
  1377.  
  1378.         /* aux_base is the start of the aux entries for this file;
  1379.            asym.index is an offset from this.  */
  1380.         aux_base = (ecoff_data (abfd)->external_aux
  1381.             + ecoffsymbol (symbol)->fdr->iauxBase);
  1382.  
  1383.         /* The aux entries are stored in host byte order; the
  1384.            order is indicated by a bit in the fdr.  */
  1385.         bigendian = ecoffsymbol (symbol)->fdr->fBigendian;
  1386.  
  1387.         /* This switch is basically from gcc/mips-tdump.c  */
  1388.         switch (ecoff_ext.asym.st)
  1389.           {
  1390.           case stNil:
  1391.           case stLabel:
  1392.         break;
  1393.  
  1394.           case stFile:
  1395.           case stBlock:
  1396.         printf ("\n      End+1 symbol: %ld", indx + sym_base);
  1397.         break;
  1398.  
  1399.           case stEnd:
  1400.         if (ecoff_ext.asym.sc == scText
  1401.             || ecoff_ext.asym.sc == scInfo)
  1402.           printf ("\n      First symbol: %ld", indx + sym_base);
  1403.         else
  1404.           printf ("\n      First symbol: %ld", 
  1405.               (AUX_GET_ISYM (bigendian,
  1406.                      &aux_base[ecoff_ext.asym.index])
  1407.                + sym_base));
  1408.         break;
  1409.  
  1410.           case stProc:
  1411.           case stStaticProc:
  1412.         if (MIPS_IS_STAB (&ecoff_ext.asym))
  1413.           ;
  1414.         else if (ecoffsymbol (symbol)->local)
  1415.           printf ("\n      End+1 symbol: %-7ld   Type:  %s",
  1416.               (AUX_GET_ISYM (bigendian,
  1417.                      &aux_base[ecoff_ext.asym.index])
  1418.                + sym_base),
  1419.               ecoff_type_to_string (abfd, aux_base, indx + 1,
  1420.                         bigendian));
  1421.         else
  1422.           printf ("\n      Local symbol: %d",
  1423.               (indx
  1424.                + sym_base
  1425.                + ecoff_data (abfd)->symbolic_header.iextMax));
  1426.         break;
  1427.  
  1428.           default:
  1429.         if (!MIPS_IS_STAB (&ecoff_ext.asym))
  1430.           printf ("\n      Type: %s",
  1431.               ecoff_type_to_string (abfd, aux_base, indx,
  1432.                         bigendian));
  1433.         break;
  1434.           }
  1435.       }
  1436.       }
  1437.       break;
  1438.     }
  1439. }
  1440.  
  1441. /* Reloc handling.  MIPS ECOFF relocs are packed into 8 bytes in
  1442.    external form.  They use a bit which indicates whether the symbol
  1443.    is external.  */
  1444.  
  1445. /* Swap a reloc in.  */
  1446.  
  1447. static void
  1448. ecoff_swap_reloc_in (abfd, ext, intern)
  1449.      bfd *abfd;
  1450.      RELOC *ext;
  1451.      struct internal_reloc *intern;
  1452. {
  1453.   intern->r_vaddr = bfd_h_get_32 (abfd, (bfd_byte *) ext->r_vaddr);
  1454.   if (abfd->xvec->header_byteorder_big_p != false)
  1455.     {
  1456.       intern->r_symndx = (((int) ext->r_bits[0]
  1457.                << RELOC_BITS0_SYMNDX_SH_LEFT_BIG)
  1458.               | ((int) ext->r_bits[1]
  1459.                  << RELOC_BITS1_SYMNDX_SH_LEFT_BIG)
  1460.               | ((int) ext->r_bits[2]
  1461.                  << RELOC_BITS2_SYMNDX_SH_LEFT_BIG));
  1462.       intern->r_type = ((ext->r_bits[3] & RELOC_BITS3_TYPE_BIG)
  1463.             >> RELOC_BITS3_TYPE_SH_BIG);
  1464.       intern->r_extern = (ext->r_bits[3] & RELOC_BITS3_EXTERN_BIG) != 0;
  1465.     }
  1466.   else
  1467.     {
  1468.       intern->r_symndx = (((int) ext->r_bits[0]
  1469.                << RELOC_BITS0_SYMNDX_SH_LEFT_LITTLE)
  1470.               | ((int) ext->r_bits[1]
  1471.                  << RELOC_BITS1_SYMNDX_SH_LEFT_LITTLE)
  1472.               | ((int) ext->r_bits[2]
  1473.                  << RELOC_BITS2_SYMNDX_SH_LEFT_LITTLE));
  1474.       intern->r_type = ((ext->r_bits[3] & RELOC_BITS3_TYPE_LITTLE)
  1475.             >> RELOC_BITS3_TYPE_SH_LITTLE);
  1476.       intern->r_extern = (ext->r_bits[3] & RELOC_BITS3_EXTERN_LITTLE) != 0;
  1477.     }
  1478. }
  1479.  
  1480. /* Swap a reloc out.  */
  1481.  
  1482. static unsigned int
  1483. ecoff_swap_reloc_out (abfd, src, dst)
  1484.      bfd *abfd;
  1485.      PTR src;
  1486.      PTR dst;
  1487. {
  1488.   struct internal_reloc *intern = (struct internal_reloc *) src;
  1489.   RELOC *ext = (RELOC *) dst;
  1490.  
  1491.   bfd_h_put_32 (abfd, intern->r_vaddr, (bfd_byte *) ext->r_vaddr);
  1492.   if (abfd->xvec->header_byteorder_big_p != false)
  1493.     {
  1494.       ext->r_bits[0] = intern->r_symndx >> RELOC_BITS0_SYMNDX_SH_LEFT_BIG;
  1495.       ext->r_bits[1] = intern->r_symndx >> RELOC_BITS1_SYMNDX_SH_LEFT_BIG;
  1496.       ext->r_bits[2] = intern->r_symndx >> RELOC_BITS2_SYMNDX_SH_LEFT_BIG;
  1497.       ext->r_bits[3] = (((intern->r_type << RELOC_BITS3_TYPE_SH_BIG)
  1498.              & RELOC_BITS3_TYPE_BIG)
  1499.             | (intern->r_extern ? RELOC_BITS3_EXTERN_BIG : 0));
  1500.     }
  1501.   else
  1502.     {
  1503.       ext->r_bits[0] = intern->r_symndx >> RELOC_BITS0_SYMNDX_SH_LEFT_LITTLE;
  1504.       ext->r_bits[1] = intern->r_symndx >> RELOC_BITS1_SYMNDX_SH_LEFT_LITTLE;
  1505.       ext->r_bits[2] = intern->r_symndx >> RELOC_BITS2_SYMNDX_SH_LEFT_LITTLE;
  1506.       ext->r_bits[3] = (((intern->r_type << RELOC_BITS3_TYPE_SH_LITTLE)
  1507.              & RELOC_BITS3_TYPE_LITTLE)
  1508.             | (intern->r_extern ? RELOC_BITS3_EXTERN_LITTLE : 0));
  1509.     }
  1510.  
  1511.   return RELSZ;
  1512. }
  1513.  
  1514. /* ECOFF relocs are either against external symbols, or against
  1515.    sections.  If we are producing relocateable output, and the reloc
  1516.    is against an external symbol, and nothing has given us any
  1517.    additional addend, the resulting reloc will also be against the
  1518.    same symbol.  In such a case, we don't want to change anything
  1519.    about the way the reloc is handled, since it will all be done at
  1520.    final link time.  Rather than put special case code into
  1521.    bfd_perform_relocation, all the reloc types use this howto
  1522.    function.  It just short circuits the reloc if producing
  1523.    relocateable output against an external symbol.  */
  1524.  
  1525. static bfd_reloc_status_type
  1526. ecoff_generic_reloc (abfd,
  1527.              reloc_entry,
  1528.              symbol,
  1529.              data,
  1530.              input_section,
  1531.              output_bfd)
  1532.      bfd *abfd;
  1533.      arelent *reloc_entry;
  1534.      asymbol *symbol;
  1535.      PTR data;
  1536.      asection *input_section;
  1537.      bfd *output_bfd;
  1538. {
  1539.   if (output_bfd != (bfd *) NULL
  1540.       && (symbol->flags & BSF_SECTION_SYM) == 0
  1541.       && reloc_entry->addend == 0)
  1542.     {
  1543.       reloc_entry->address += input_section->output_offset;
  1544.       return bfd_reloc_ok;
  1545.     }
  1546.  
  1547.   return bfd_reloc_continue;
  1548. }
  1549.  
  1550. /* Do a REFHI relocation.  This has to be done in combination with a
  1551.    REFLO reloc, because there is a carry from the REFLO to the REFHI.
  1552.    Here we just save the information we need; we do the actual
  1553.    relocation when we see the REFLO.  ECOFF requires that the REFLO
  1554.    immediately follow the REFHI, so this ought to work.  */
  1555.  
  1556. static bfd_byte *ecoff_refhi_addr;
  1557. static bfd_vma ecoff_refhi_addend;
  1558.  
  1559. static bfd_reloc_status_type
  1560. ecoff_refhi_reloc (abfd,
  1561.            reloc_entry,
  1562.            symbol,
  1563.            data,
  1564.            input_section,
  1565.            output_bfd)
  1566.      bfd *abfd;
  1567.      arelent *reloc_entry;
  1568.      asymbol *symbol;
  1569.      PTR data;
  1570.      asection *input_section;
  1571.      bfd *output_bfd;
  1572. {
  1573.   bfd_reloc_status_type ret;
  1574.   bfd_vma relocation;
  1575.  
  1576.   /* If we're relocating, and this an external symbol, we don't want
  1577.      to change anything.  */
  1578.   if (output_bfd != (bfd *) NULL
  1579.       && (symbol->flags & BSF_SECTION_SYM) == 0
  1580.       && reloc_entry->addend == 0)
  1581.     {
  1582.       reloc_entry->address += input_section->output_offset;
  1583.       return bfd_reloc_ok;
  1584.     }
  1585.  
  1586.   ret = bfd_reloc_ok;
  1587.   if (symbol->section == &bfd_und_section
  1588.       && output_bfd == (bfd *) NULL)
  1589.     ret = bfd_reloc_undefined;
  1590.  
  1591.   if (bfd_is_com_section (symbol->section))
  1592.     relocation = 0;
  1593.   else
  1594.     relocation = symbol->value;
  1595.  
  1596.   relocation += symbol->section->output_section->vma;
  1597.   relocation += symbol->section->output_offset;
  1598.   relocation += reloc_entry->addend;
  1599.  
  1600.   if (reloc_entry->address > input_section->_cooked_size)
  1601.     return bfd_reloc_outofrange;
  1602.  
  1603.   /* Save the information, and let REFLO do the actual relocation.  */
  1604.   ecoff_refhi_addr = (bfd_byte *) data + reloc_entry->address;
  1605.   ecoff_refhi_addend = relocation;
  1606.  
  1607.   if (output_bfd != (bfd *) NULL)
  1608.     reloc_entry->address += input_section->output_offset;
  1609.  
  1610.   return ret;
  1611. }
  1612.  
  1613. /* Do a REFLO relocation.  This is a straightforward 16 bit inplace
  1614.    relocation; this function exists in order to do the REFHI
  1615.    relocation described above.  */
  1616.  
  1617. static bfd_reloc_status_type
  1618. ecoff_reflo_reloc (abfd,
  1619.            reloc_entry,
  1620.            symbol,
  1621.            data,
  1622.            input_section,
  1623.            output_bfd)
  1624.      bfd *abfd;
  1625.      arelent *reloc_entry;
  1626.      asymbol *symbol;
  1627.      PTR data;
  1628.      asection *input_section;
  1629.      bfd *output_bfd;
  1630. {
  1631.   if (ecoff_refhi_addr != (bfd_byte *) NULL)
  1632.     {
  1633.       unsigned long insn;
  1634.       unsigned long val;
  1635.       unsigned long vallo;
  1636.  
  1637.       /* Do the REFHI relocation.  Note that we actually don't need to
  1638.      know anything about the REFLO itself, except where to find
  1639.      the low 16 bits of the addend needed by the REFHI.  */
  1640.       insn = bfd_get_32 (abfd, ecoff_refhi_addr);
  1641.       vallo = (bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address)
  1642.            & 0xffff);
  1643.       val = ((insn & 0xffff) << 16) + vallo;
  1644.       val += ecoff_refhi_addend;
  1645.  
  1646.       /* The low order 16 bits are always treated as a signed value.
  1647.      Therefore, a negative value in the low order bits requires an
  1648.      adjustment in the high order bits.  We need to make this
  1649.      adjustment in two ways: once for the bits we took from the
  1650.      data, and once for the bits we are putting back in to the
  1651.      data.  */
  1652.       if ((vallo & 0x8000) != 0)
  1653.     val -= 0x10000;
  1654.       if ((val & 0x8000) != 0)
  1655.     val += 0x10000;
  1656.  
  1657.       insn = (insn &~ 0xffff) | ((val >> 16) & 0xffff);
  1658.       bfd_put_32 (abfd, insn, ecoff_refhi_addr);
  1659.  
  1660.       ecoff_refhi_addr = (bfd_byte *) NULL;
  1661.     }
  1662.  
  1663.   /* Now do the REFLO reloc in the usual way.  */
  1664.   return ecoff_generic_reloc (abfd, reloc_entry, symbol, data,
  1665.                   input_section, output_bfd);
  1666. }
  1667.  
  1668. /* Do a GPREL relocation.  This is a 16 bit value which must become
  1669.    the offset from the gp register.  */
  1670.  
  1671. static bfd_reloc_status_type
  1672. ecoff_gprel_reloc (abfd,
  1673.            reloc_entry,
  1674.            symbol,
  1675.            data,
  1676.            input_section,
  1677.            output_bfd)
  1678.      bfd *abfd;
  1679.      arelent *reloc_entry;
  1680.      asymbol *symbol;
  1681.      PTR data;
  1682.      asection *input_section;
  1683.      bfd *output_bfd;
  1684. {
  1685.   boolean relocateable;
  1686.   bfd_vma relocation;
  1687.   unsigned long val;
  1688.   unsigned long insn;
  1689.  
  1690.   /* If we're relocating, and this is an external symbol with no
  1691.      addend, we don't want to change anything.  We will only have an
  1692.      addend if this is a newly created reloc, not read from an ECOFF
  1693.      file.  */
  1694.   if (output_bfd != (bfd *) NULL
  1695.       && (symbol->flags & BSF_SECTION_SYM) == 0
  1696.       && reloc_entry->addend == 0)
  1697.     {
  1698.       reloc_entry->address += input_section->output_offset;
  1699.       return bfd_reloc_ok;
  1700.     }
  1701.  
  1702.   if (output_bfd != (bfd *) NULL)
  1703.     relocateable = true;
  1704.   else
  1705.     {
  1706.       relocateable = false;
  1707.       output_bfd = symbol->section->output_section->owner;
  1708.     }
  1709.  
  1710.   if (symbol->section == &bfd_und_section
  1711.       && relocateable == false)
  1712.     return bfd_reloc_undefined;
  1713.  
  1714.   /* We have to figure out the gp value, so that we can adjust the
  1715.      symbol value correctly.  We look up the symbol _gp in the output
  1716.      BFD.  If we can't find it, we're stuck.  We cache it in the ECOFF
  1717.      target data.  We don't need to adjust the symbol value for an
  1718.      external symbol if we are producing relocateable output.  */
  1719.   if (ecoff_data (output_bfd)->gp == 0
  1720.       && (relocateable == false
  1721.       || (symbol->flags & BSF_SECTION_SYM) != 0))
  1722.     {
  1723.       if (relocateable != false)
  1724.     {
  1725.       /* Make up a value.  */
  1726.       ecoff_data (output_bfd)->gp =
  1727.         symbol->section->output_section->vma + 0x4000;
  1728.     }
  1729.       else
  1730.     {
  1731.       unsigned int count;
  1732.       asymbol **sym;
  1733.       unsigned int i;
  1734.  
  1735.       count = bfd_get_symcount (output_bfd);
  1736.       sym = bfd_get_outsymbols (output_bfd);
  1737.  
  1738.       /* We should do something more friendly here, but we don't
  1739.          have a good reloc status to return.  */
  1740.       if (sym == (asymbol **) NULL)
  1741.         abort ();
  1742.  
  1743.       for (i = 0; i < count; i++, sym++)
  1744.         {
  1745.           register CONST char *name;
  1746.  
  1747.           name = bfd_asymbol_name (*sym);
  1748.           if (*name == '_' && strcmp (name, "_gp") == 0)
  1749.         {
  1750.           ecoff_data (output_bfd)->gp = bfd_asymbol_value (*sym);
  1751.           break;
  1752.         }
  1753.         }
  1754.  
  1755.       /* We should do something more friendly here, but we don't have
  1756.          a good reloc status to return.  */
  1757.       if (i >= count)
  1758.         abort ();
  1759.     }
  1760.     }
  1761.  
  1762.   if (bfd_is_com_section (symbol->section))
  1763.     relocation = 0;
  1764.   else
  1765.     relocation = symbol->value;
  1766.  
  1767.   relocation += symbol->section->output_section->vma;
  1768.   relocation += symbol->section->output_offset;
  1769.  
  1770.   if (reloc_entry->address > input_section->_cooked_size)
  1771.     return bfd_reloc_outofrange;
  1772.  
  1773.   insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
  1774.  
  1775.   /* Set val to the offset into the section or symbol.  */
  1776.   val = ((insn & 0xffff) + reloc_entry->addend) & 0xffff;
  1777.   if (val & 0x8000)
  1778.     val -= 0x10000;
  1779.  
  1780.   /* Adjust val for the final section location and GP value.  If we
  1781.      are producing relocateable output, we don't want to do this for
  1782.      an external symbol.  */
  1783.   if (relocateable == false
  1784.       || (symbol->flags & BSF_SECTION_SYM) != 0)
  1785.     val += relocation - ecoff_data (output_bfd)->gp;
  1786.  
  1787.   insn = (insn &~ 0xffff) | (val & 0xffff);
  1788.   bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
  1789.  
  1790.   if (relocateable != false)
  1791.     reloc_entry->address += input_section->output_offset;
  1792.  
  1793.   /* Make sure it fit in 16 bits.  */
  1794.   if (val >= 0x8000 && val < 0xffff8000)
  1795.     return bfd_reloc_outofrange;
  1796.  
  1797.   return bfd_reloc_ok;
  1798. }
  1799.  
  1800. /* How to process the various relocs types.  */
  1801.  
  1802. static reloc_howto_type ecoff_howto_table[] =
  1803. {
  1804.   /* Reloc type 0 is ignored.  The reloc reading code ensures that
  1805.      this is a reference to the .abs section, which will cause
  1806.      bfd_perform_relocation to do nothing.  */
  1807.   HOWTO (ECOFF_R_IGNORE,    /* type */
  1808.      0,            /* rightshift */
  1809.      0,            /* size (0 = byte, 1 = short, 2 = long) */
  1810.      8,            /* bitsize (obsolete) */
  1811.      false,            /* pc_relative */
  1812.      0,            /* bitpos */
  1813.      false,            /* absolute (obsolete) */
  1814.      false,            /* complain_on_overflow */
  1815.      0,            /* special_function */
  1816.      "IGNORE",        /* name */
  1817.      false,            /* partial_inplace */
  1818.      0,            /* src_mask */
  1819.      0,            /* dst_mask */
  1820.      false),        /* pcrel_offset */
  1821.  
  1822.   /* A 16 bit reference to a symbol, normally from a data section.  */
  1823.   HOWTO (ECOFF_R_REFHALF,    /* type */
  1824.      0,            /* rightshift */
  1825.      1,            /* size (0 = byte, 1 = short, 2 = long) */
  1826.      16,            /* bitsize (obsolete) */
  1827.      false,            /* pc_relative */
  1828.      0,            /* bitpos */
  1829.      false,            /* absolute (obsolete) */
  1830.      true,            /* complain_on_overflow */
  1831.      ecoff_generic_reloc,    /* special_function */
  1832.      "REFHALF",        /* name */
  1833.      true,            /* partial_inplace */
  1834.      0xffff,        /* src_mask */
  1835.      0xffff,        /* dst_mask */
  1836.      false),        /* pcrel_offset */
  1837.  
  1838.   /* A 32 bit reference to a symbol, normally from a data section.  */
  1839.   HOWTO (ECOFF_R_REFWORD,    /* type */
  1840.      0,            /* rightshift */
  1841.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  1842.      32,            /* bitsize (obsolete) */
  1843.      false,            /* pc_relative */
  1844.      0,            /* bitpos */
  1845.      false,            /* absolute (obsolete) */
  1846.      true,            /* complain_on_overflow */
  1847.      ecoff_generic_reloc,    /* special_function */
  1848.      "REFWORD",        /* name */
  1849.      true,            /* partial_inplace */
  1850.      0xffffffff,        /* src_mask */
  1851.      0xffffffff,        /* dst_mask */
  1852.      false),        /* pcrel_offset */
  1853.  
  1854.   /* A 26 bit absolute jump address.  */
  1855.   HOWTO (ECOFF_R_JMPADDR,    /* type */
  1856.      2,            /* rightshift */
  1857.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  1858.      32,            /* bitsize (obsolete) */
  1859.      false,            /* pc_relative */
  1860.      0,            /* bitpos */
  1861.      false,            /* absolute (obsolete) */
  1862.      true,            /* complain_on_overflow */
  1863.      ecoff_generic_reloc,    /* special_function */
  1864.      "JMPADDR",        /* name */
  1865.      true,            /* partial_inplace */
  1866.      0x3ffffff,        /* src_mask */
  1867.      0x3ffffff,        /* dst_mask */
  1868.      false),        /* pcrel_offset */
  1869.  
  1870.   /* The high 16 bits of a symbol value.  Handled by the function
  1871.      ecoff_refhi_reloc.  */
  1872.   HOWTO (ECOFF_R_REFHI,        /* type */
  1873.      16,            /* rightshift */
  1874.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  1875.      32,            /* bitsize (obsolete) */
  1876.      false,            /* pc_relative */
  1877.      0,            /* bitpos */
  1878.      false,            /* absolute (obsolete) */
  1879.      true,            /* complain_on_overflow */
  1880.      ecoff_refhi_reloc,    /* special_function */
  1881.      "REFHI",        /* name */
  1882.      true,            /* partial_inplace */
  1883.      0xffff,        /* src_mask */
  1884.      0xffff,        /* dst_mask */
  1885.      false),        /* pcrel_offset */
  1886.  
  1887.   /* The low 16 bits of a symbol value.  */
  1888.   HOWTO (ECOFF_R_REFLO,        /* type */
  1889.      0,            /* rightshift */
  1890.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  1891.      32,            /* bitsize (obsolete) */
  1892.      false,            /* pc_relative */
  1893.      0,            /* bitpos */
  1894.      false,            /* absolute (obsolete) */
  1895.      true,            /* complain_on_overflow */
  1896.      ecoff_reflo_reloc,    /* special_function */
  1897.      "REFLO",        /* name */
  1898.      true,            /* partial_inplace */
  1899.      0xffff,        /* src_mask */
  1900.      0xffff,        /* dst_mask */
  1901.      false),        /* pcrel_offset */
  1902.  
  1903.   /* A reference to an offset from the gp register.  Handled by the
  1904.      function ecoff_gprel_reloc.  */
  1905.   HOWTO (ECOFF_R_GPREL,        /* type */
  1906.      0,            /* rightshift */
  1907.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  1908.      32,            /* bitsize (obsolete) */
  1909.      false,            /* pc_relative */
  1910.      0,            /* bitpos */
  1911.      false,            /* absolute (obsolete) */
  1912.      true,            /* complain_on_overflow */
  1913.      ecoff_gprel_reloc,    /* special_function */
  1914.      "GPREL",        /* name */
  1915.      true,            /* partial_inplace */
  1916.      0xffff,        /* src_mask */
  1917.      0xffff,        /* dst_mask */
  1918.      false),        /* pcrel_offset */
  1919.  
  1920.   /* A reference to a literal using an offset from the gp register.
  1921.      Handled by the function ecoff_gprel_reloc.  */
  1922.   HOWTO (ECOFF_R_LITERAL,    /* type */
  1923.      0,            /* rightshift */
  1924.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  1925.      32,            /* bitsize (obsolete) */
  1926.      false,            /* pc_relative */
  1927.      0,            /* bitpos */
  1928.      false,            /* absolute (obsolete) */
  1929.      true,            /* complain_on_overflow */
  1930.      ecoff_gprel_reloc,    /* special_function */
  1931.      "LITERAL",        /* name */
  1932.      true,            /* partial_inplace */
  1933.      0xffff,        /* src_mask */
  1934.      0xffff,        /* dst_mask */
  1935.      false)            /* pcrel_offset */
  1936. };
  1937.  
  1938. #define ECOFF_HOWTO_COUNT \
  1939.   (sizeof ecoff_howto_table / sizeof ecoff_howto_table[0])
  1940.  
  1941. /* Read in the relocs for a section.  */
  1942.  
  1943. static boolean
  1944. ecoff_slurp_reloc_table (abfd, section, symbols)
  1945.      bfd *abfd;
  1946.      asection *section;
  1947.      asymbol **symbols;
  1948. {
  1949.   RELOC *external_relocs;
  1950.   arelent *internal_relocs;
  1951.   arelent *rptr;
  1952.   unsigned int i;
  1953.  
  1954.   if (section->relocation != (arelent *) NULL
  1955.       || section->reloc_count == 0
  1956.       || (section->flags & SEC_CONSTRUCTOR) != 0)
  1957.     return true;
  1958.  
  1959.   if (ecoff_slurp_symbol_table (abfd) == false)
  1960.     return false;
  1961.   
  1962.   internal_relocs = (arelent *) bfd_alloc (abfd,
  1963.                        (sizeof (arelent)
  1964.                         * section->reloc_count));
  1965.   external_relocs = (RELOC *) bfd_alloc (abfd, RELSZ * section->reloc_count);
  1966.   if (internal_relocs == (arelent *) NULL
  1967.       || external_relocs == (RELOC *) NULL)
  1968.     {
  1969.       bfd_error = no_memory;
  1970.       return false;
  1971.     }
  1972.   if (bfd_seek (abfd, section->rel_filepos, SEEK_SET) != 0)
  1973.     return false;
  1974.   if (bfd_read (external_relocs, 1, RELSZ * section->reloc_count, abfd)
  1975.       != RELSZ * section->reloc_count)
  1976.     {
  1977.       bfd_error = system_call_error;
  1978.       return false;
  1979.     }
  1980.  
  1981.   for (i = 0, rptr = internal_relocs; i < section->reloc_count; i++, rptr++)
  1982.     {
  1983.       struct internal_reloc intern;
  1984.  
  1985.       ecoff_swap_reloc_in (abfd, external_relocs + i, &intern);
  1986.  
  1987.       if (intern.r_type > ECOFF_R_LITERAL)
  1988.     abort ();
  1989.  
  1990.       if (intern.r_extern)
  1991.     {
  1992.       /* r_symndx is an index into the external symbols.  */
  1993.       BFD_ASSERT (intern.r_symndx >= 0
  1994.               && (intern.r_symndx
  1995.               < ecoff_data (abfd)->symbolic_header.iextMax));
  1996.       rptr->sym_ptr_ptr = symbols + intern.r_symndx;
  1997.       rptr->addend = 0;
  1998.     }
  1999.       else
  2000.     {
  2001.       CONST char *sec_name;
  2002.       asection *sec;
  2003.  
  2004.       /* r_symndx is a section key.  */
  2005.       switch (intern.r_symndx)
  2006.         {
  2007.         case RELOC_SECTION_TEXT:  sec_name = ".text";  break;
  2008.         case RELOC_SECTION_RDATA: sec_name = ".rdata"; break;
  2009.         case RELOC_SECTION_DATA:  sec_name = ".data";  break;
  2010.         case RELOC_SECTION_SDATA: sec_name = ".sdata"; break;
  2011.         case RELOC_SECTION_SBSS:  sec_name = ".sbss";  break;
  2012.         case RELOC_SECTION_BSS:   sec_name = ".bss";   break;
  2013.         case RELOC_SECTION_INIT:  sec_name = ".init";  break;
  2014.         case RELOC_SECTION_LIT8:  sec_name = ".lit8";  break;
  2015.         case RELOC_SECTION_LIT4:  sec_name = ".lit4";  break;
  2016.         default: abort ();
  2017.         }
  2018.  
  2019.       sec = bfd_get_section_by_name (abfd, sec_name);
  2020.       if (sec == (asection *) NULL)
  2021.         abort ();
  2022.       rptr->sym_ptr_ptr = sec->symbol_ptr_ptr;
  2023.  
  2024.       rptr->addend = - bfd_get_section_vma (abfd, sec);
  2025.       if (intern.r_type == ECOFF_R_GPREL
  2026.           || intern.r_type == ECOFF_R_LITERAL)
  2027.         rptr->addend += ecoff_data (abfd)->gp;
  2028.     }
  2029.  
  2030.       rptr->address = intern.r_vaddr - bfd_get_section_vma (abfd, section);
  2031.       rptr->howto = &ecoff_howto_table[intern.r_type];
  2032.  
  2033.       /* If the type is ECOFF_R_IGNORE, make sure this is a reference
  2034.      to the absolute section so that the reloc is ignored.  */
  2035.       if (intern.r_type == ECOFF_R_IGNORE)
  2036.     rptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
  2037.     }
  2038.  
  2039.   bfd_release (abfd, external_relocs);
  2040.  
  2041.   section->relocation = internal_relocs;
  2042.  
  2043.   return true;
  2044. }
  2045.  
  2046. /* Get a canonical list of relocs.  */
  2047.  
  2048. static unsigned int
  2049. ecoff_canonicalize_reloc (abfd, section, relptr, symbols)
  2050.      bfd *abfd;
  2051.      asection *section;
  2052.      arelent **relptr;
  2053.      asymbol **symbols;
  2054. {
  2055.   unsigned int count;
  2056.  
  2057.   if (section->flags & SEC_CONSTRUCTOR) 
  2058.     {
  2059.       arelent_chain *chain;
  2060.  
  2061.       /* This section has relocs made up by us, not the file, so take
  2062.      them out of their chain and place them into the data area
  2063.      provided.  */
  2064.       for (count = 0, chain = section->constructor_chain;
  2065.        count < section->reloc_count;
  2066.        count++, chain = chain->next)
  2067.     *relptr++ = &chain->relent;
  2068.     }
  2069.   else
  2070.     { 
  2071.       arelent *tblptr;
  2072.  
  2073.       if (ecoff_slurp_reloc_table (abfd, section, symbols) == false)
  2074.     return 0;
  2075.  
  2076.       tblptr = section->relocation;
  2077.       if (tblptr == (arelent *) NULL)
  2078.     return 0;
  2079.  
  2080.       for (count = 0; count < section->reloc_count; count++)
  2081.     *relptr++ = tblptr++;
  2082.     }
  2083.  
  2084.   *relptr = (arelent *) NULL;
  2085.  
  2086.   return section->reloc_count;
  2087. }
  2088.  
  2089. /* Get the howto structure for a generic reloc type.  */
  2090.  
  2091. static CONST struct reloc_howto_struct *
  2092. ecoff_bfd_reloc_type_lookup (abfd, code)
  2093.      bfd *abfd;
  2094.      bfd_reloc_code_real_type code;
  2095. {
  2096.   int ecoff_type;
  2097.  
  2098.   switch (code)
  2099.     {
  2100.     case BFD_RELOC_16:
  2101.       ecoff_type = ECOFF_R_REFHALF;
  2102.       break;
  2103.     case BFD_RELOC_32:
  2104.       ecoff_type = ECOFF_R_REFWORD;
  2105.       break;
  2106.     case BFD_RELOC_MIPS_JMP:
  2107.       ecoff_type = ECOFF_R_JMPADDR;
  2108.       break;
  2109.     case BFD_RELOC_HI16_S:
  2110.       ecoff_type = ECOFF_R_REFHI;
  2111.       break;
  2112.     case BFD_RELOC_LO16:
  2113.       ecoff_type = ECOFF_R_REFLO;
  2114.       break;
  2115.     case BFD_RELOC_MIPS_GPREL:
  2116.       ecoff_type = ECOFF_R_GPREL;
  2117.       break;
  2118.     default:
  2119.       return (CONST struct reloc_howto_struct *) NULL;
  2120.     }
  2121.  
  2122.   return &ecoff_howto_table[ecoff_type];
  2123. }
  2124.  
  2125. /* Provided a BFD, a section and an offset into the section, calculate
  2126.    and return the name of the source file and the line nearest to the
  2127.    wanted location.  */
  2128.  
  2129. static boolean
  2130. ecoff_find_nearest_line (abfd,
  2131.              section,
  2132.              ignore_symbols,
  2133.              offset,
  2134.              filename_ptr,
  2135.              functionname_ptr,
  2136.              retline_ptr)
  2137.      bfd *abfd;
  2138.      asection *section;
  2139.      asymbol **ignore_symbols;
  2140.      bfd_vma offset;
  2141.      CONST char **filename_ptr;
  2142.      CONST char **functionname_ptr;
  2143.      unsigned int *retline_ptr;
  2144. {
  2145.   FDR *fdr_ptr;
  2146.   FDR *fdr_start;
  2147.   FDR *fdr_end;
  2148.   FDR *fdr_hold;
  2149.   struct pdr_ext *pdr_ptr;
  2150.   struct pdr_ext *pdr_end;
  2151.   PDR pdr;
  2152.   unsigned char *line_ptr;
  2153.   unsigned char *line_end;
  2154.   int lineno;
  2155.  
  2156.   /* If we're not in the .text section, we don't have any line
  2157.      numbers.  */
  2158.   if (strcmp (section->name, _TEXT) != 0
  2159.       || offset < ecoff_data (abfd)->text_start
  2160.       || offset >= ecoff_data (abfd)->text_end)
  2161.     return false;
  2162.  
  2163.   /* Make sure we have the FDR's.  */
  2164.   if (ecoff_slurp_symbolic_info (abfd) == false
  2165.       || bfd_get_symcount (abfd) == 0)
  2166.     return false;
  2167.  
  2168.   /* Each file descriptor (FDR) has a memory address.  Here we track
  2169.      down which FDR we want.  The FDR's are stored in increasing
  2170.      memory order.  If speed is ever important, this can become a
  2171.      binary search.  We must ignore FDR's with no PDR entries; they
  2172.      will have the adr of the FDR before or after them.  */
  2173.   fdr_start = ecoff_data (abfd)->fdr;
  2174.   fdr_end = fdr_start + ecoff_data (abfd)->symbolic_header.ifdMax;
  2175.   fdr_hold = (FDR *) NULL;
  2176.   for (fdr_ptr = fdr_start; fdr_ptr < fdr_end; fdr_ptr++)
  2177.     {
  2178.       if (fdr_ptr->cpd == 0)
  2179.     continue;
  2180.       if (offset < fdr_ptr->adr)
  2181.     break;
  2182.       fdr_hold = fdr_ptr;
  2183.     }
  2184.   if (fdr_hold == (FDR *) NULL)
  2185.     return false;
  2186.   fdr_ptr = fdr_hold;
  2187.  
  2188.   /* Each FDR has a list of procedure descriptors (PDR).  PDR's also
  2189.      have an address, which is relative to the FDR address, and are
  2190.      also stored in increasing memory order.  */
  2191.   offset -= fdr_ptr->adr;
  2192.   pdr_ptr = ecoff_data (abfd)->external_pdr + fdr_ptr->ipdFirst;
  2193.   pdr_end = pdr_ptr + fdr_ptr->cpd;
  2194.   ecoff_swap_pdr_in (abfd, pdr_ptr, &pdr);
  2195.  
  2196.   /* The address of the first PDR is an offset which applies to the
  2197.      addresses of all the PDR's.  */
  2198.   offset += pdr.adr;
  2199.  
  2200.   for (pdr_ptr++; pdr_ptr < pdr_end; pdr_ptr++)
  2201.     {
  2202.       ecoff_swap_pdr_in (abfd, pdr_ptr, &pdr);
  2203.       if (offset < pdr.adr)
  2204.     break;
  2205.     }
  2206.  
  2207.   /* Now we can look for the actual line number.  The line numbers are
  2208.      stored in a very funky format, which I won't try to describe.
  2209.      Note that right here pdr_ptr and pdr hold the PDR *after* the one
  2210.      we want; we need this to compute line_end.  */
  2211.   line_end = ecoff_data (abfd)->line;
  2212.   if (pdr_ptr == pdr_end)
  2213.     line_end += fdr_ptr->cbLineOffset + fdr_ptr->cbLine;
  2214.   else
  2215.     line_end += fdr_ptr->cbLineOffset + pdr.cbLineOffset;
  2216.  
  2217.   /* Now change pdr and pdr_ptr to the one we want.  */
  2218.   pdr_ptr--;
  2219.   ecoff_swap_pdr_in (abfd, pdr_ptr, &pdr);
  2220.  
  2221.   offset -= pdr.adr;
  2222.   lineno = pdr.lnLow;
  2223.   line_ptr = (ecoff_data (abfd)->line
  2224.           + fdr_ptr->cbLineOffset
  2225.           + pdr.cbLineOffset);
  2226.   while (line_ptr < line_end)
  2227.     {
  2228.       int delta;
  2229.       int count;
  2230.  
  2231.       delta = *line_ptr >> 4;
  2232.       if (delta >= 0x8)
  2233.     delta -= 0x10;
  2234.       count = (*line_ptr & 0xf) + 1;
  2235.       ++line_ptr;
  2236.       if (delta == -8)
  2237.     {
  2238.       delta = (((line_ptr[0]) & 0xff) << 8) + ((line_ptr[1]) & 0xff);
  2239.       if (delta >= 0x8000)
  2240.         delta -= 0x10000;
  2241.       line_ptr += 2;
  2242.     }
  2243.       lineno += delta;
  2244.       if (offset < count * 4)
  2245.     break;
  2246.       offset -= count * 4;
  2247.     }
  2248.  
  2249.   /* If fdr_ptr->rss is -1, then this file does not have full symbols,
  2250.      at least according to gdb/mipsread.c.  */
  2251.   if (fdr_ptr->rss == -1)
  2252.     {
  2253.       *filename_ptr = NULL;
  2254.       if (pdr.isym == -1)
  2255.     *functionname_ptr = NULL;
  2256.       else
  2257.     {
  2258.       EXTR proc_ext;
  2259.  
  2260.       ecoff_swap_ext_in (abfd,
  2261.                  (ecoff_data (abfd)->external_ext
  2262.                   + pdr.isym),
  2263.                  &proc_ext);
  2264.       *functionname_ptr = ecoff_data (abfd)->ssext + proc_ext.asym.iss;
  2265.     }
  2266.     }
  2267.   else
  2268.     {
  2269.       SYMR proc_sym;
  2270.  
  2271.       *filename_ptr = ecoff_data (abfd)->ss + fdr_ptr->issBase + fdr_ptr->rss;
  2272.       ecoff_swap_sym_in (abfd,
  2273.              (ecoff_data (abfd)->external_sym
  2274.               + fdr_ptr->isymBase
  2275.               + pdr.isym),
  2276.              &proc_sym);
  2277.       *functionname_ptr = (ecoff_data (abfd)->ss
  2278.                + fdr_ptr->issBase
  2279.                + proc_sym.iss);
  2280.     }
  2281.   *retline_ptr = lineno;
  2282.   return true;
  2283. }
  2284.  
  2285. /* We can't use the generic linking routines for ECOFF, because we
  2286.    have to handle all the debugging information.  The generic link
  2287.    routine just works out the section contents and attaches a list of
  2288.    symbols.
  2289.  
  2290.    We link by looping over all the seclets.  We make two passes.  On
  2291.    the first we set the actual section contents and determine the size
  2292.    of the debugging information.  On the second we accumulate the
  2293.    debugging information and write it out.
  2294.  
  2295.    This currently always accumulates the debugging information, which
  2296.    is incorrect, because it ignores the -s and -S options of the
  2297.    linker.  The linker needs to be modified to give us that
  2298.    information in a more useful format (currently it just provides a
  2299.    list of symbols which should appear in the output file).  */
  2300.  
  2301. /* Clear the output_has_begun flag for all the input BFD's.  We use it
  2302.    to avoid linking in the debugging information for a BFD more than
  2303.    once.  */
  2304.  
  2305. static void
  2306. ecoff_clear_output_flags (abfd)
  2307.      bfd *abfd;
  2308. {
  2309.   register asection *o;
  2310.   register bfd_seclet_type *p;
  2311.  
  2312.   for (o = abfd->sections; o != (asection *) NULL; o = o->next)
  2313.     for (p = o->seclets_head;
  2314.      p != (bfd_seclet_type *) NULL;
  2315.      p = p->next)
  2316.       if (p->type == bfd_indirect_seclet)
  2317.     p->u.indirect.section->owner->output_has_begun = false;
  2318. }
  2319.  
  2320. /* Handle an indirect seclet on the first pass.  Set the contents of
  2321.    the output section, and accumulate the debugging information if
  2322.    any.  */
  2323.  
  2324. static boolean
  2325. ecoff_rel (output_bfd, seclet, output_section, data, relocateable)
  2326.      bfd *output_bfd;
  2327.      bfd_seclet_type *seclet;
  2328.      asection *output_section;
  2329.      PTR data;
  2330.      boolean relocateable;
  2331. {
  2332.   bfd *input_bfd;
  2333.   HDRR *output_symhdr;
  2334.   HDRR *input_symhdr;
  2335.  
  2336.   if ((output_section->flags & SEC_HAS_CONTENTS)
  2337.       && !(output_section->flags & SEC_NEVER_LOAD)
  2338.       && (output_section->flags & SEC_LOAD)
  2339.       && seclet->size)
  2340.     {
  2341.       data = (PTR) bfd_get_relocated_section_contents (output_bfd,
  2342.                                seclet,
  2343.                                data,
  2344.                                relocateable);
  2345.       if (bfd_set_section_contents (output_bfd,
  2346.                     output_section,
  2347.                     data,
  2348.                     seclet->offset,
  2349.                     seclet->size)
  2350.       == false)
  2351.     {
  2352.       abort();
  2353.     }
  2354.     }
  2355.  
  2356.   input_bfd = seclet->u.indirect.section->owner;
  2357.  
  2358.   /* We want to figure out how much space will be required to
  2359.      incorporate all the debugging information from input_bfd.  We use
  2360.      the output_has_begun field to avoid adding it in more than once.
  2361.      The actual incorporation is done in the second pass, in
  2362.      ecoff_get_debug.  The code has to parallel that code in its
  2363.      manipulations of output_symhdr.  */
  2364.  
  2365.   if (input_bfd->output_has_begun)
  2366.     return true;
  2367.   input_bfd->output_has_begun = true;
  2368.  
  2369.   output_symhdr = &ecoff_data (output_bfd)->symbolic_header;
  2370.  
  2371.   if (input_bfd->xvec->flavour != bfd_target_ecoff_flavour)
  2372.     {
  2373.       asymbol **symbols;
  2374.       asymbol **sym_ptr;
  2375.       asymbol **sym_end;
  2376.  
  2377.       /* We just accumulate local symbols from a non-ECOFF BFD.  The
  2378.      external symbols are handled separately.  */
  2379.  
  2380.       symbols = (asymbol **) bfd_alloc (output_bfd,
  2381.                     get_symtab_upper_bound (input_bfd));
  2382.       if (symbols == (asymbol **) NULL)
  2383.     {
  2384.       bfd_error = no_memory;
  2385.       return false;
  2386.     }
  2387.       sym_end = symbols + bfd_canonicalize_symtab (input_bfd, symbols);
  2388.  
  2389.       for (sym_ptr = symbols; sym_ptr < sym_end; sym_ptr++)
  2390.     {
  2391.       size_t len;
  2392.  
  2393.       len = strlen ((*sym_ptr)->name);
  2394.       if (((*sym_ptr)->flags & BSF_EXPORT) == 0)
  2395.         {
  2396.           ++output_symhdr->isymMax;
  2397.           output_symhdr->issMax += len + 1;
  2398.         }
  2399.     }
  2400.  
  2401.       bfd_release (output_bfd, (PTR) symbols);
  2402.  
  2403.       ++output_symhdr->ifdMax;
  2404.  
  2405.       return true;
  2406.     }
  2407.  
  2408.   /* We simply add in the information from another ECOFF BFD.  First
  2409.      we make sure we have the symbolic information.  */
  2410.   if (ecoff_slurp_symbol_table (input_bfd) == false)
  2411.     return false;
  2412.   if (bfd_get_symcount (input_bfd) == 0)
  2413.     return true;
  2414.  
  2415.   input_symhdr = &ecoff_data (input_bfd)->symbolic_header;
  2416.  
  2417.   /* Figure out how much information we are going to be putting in.
  2418.      The external symbols are handled separately.  */
  2419.   output_symhdr->ilineMax += input_symhdr->ilineMax;
  2420.   output_symhdr->cbLine += input_symhdr->cbLine;
  2421.   output_symhdr->idnMax += input_symhdr->idnMax;
  2422.   output_symhdr->ipdMax += input_symhdr->ipdMax;
  2423.   output_symhdr->isymMax += input_symhdr->isymMax;
  2424.   output_symhdr->ioptMax += input_symhdr->ioptMax;
  2425.   output_symhdr->iauxMax += input_symhdr->iauxMax;
  2426.   output_symhdr->issMax += input_symhdr->issMax;
  2427.   output_symhdr->ifdMax += input_symhdr->ifdMax;
  2428.  
  2429.   /* The RFD's are special, since we create them if needed.  */
  2430.   if (input_symhdr->crfd > 0)
  2431.     output_symhdr->crfd += input_symhdr->crfd;
  2432.   else
  2433.     output_symhdr->crfd += input_symhdr->ifdMax;
  2434.  
  2435.   return true;
  2436. }
  2437.  
  2438. /* Handle an arbitrary seclet on the first pass.  */
  2439.  
  2440. static boolean
  2441. ecoff_dump_seclet (abfd, seclet, section, data, relocateable)
  2442.      bfd *abfd;
  2443.      bfd_seclet_type *seclet;
  2444.      asection *section;
  2445.      PTR data;
  2446.      boolean relocateable;
  2447. {
  2448.   switch (seclet->type) 
  2449.     {
  2450.     case bfd_indirect_seclet:
  2451.       /* The contents of this section come from another one somewhere
  2452.      else.  */
  2453.       return ecoff_rel (abfd, seclet, section, data, relocateable);
  2454.  
  2455.     case bfd_fill_seclet:
  2456.       /* Fill in the section with fill.value.  This is used to pad out
  2457.      sections, but we must avoid padding the .bss section.  */
  2458.       if ((section->flags & SEC_HAS_CONTENTS) == 0)
  2459.     {
  2460.       if (seclet->u.fill.value != 0)
  2461.         abort ();
  2462.     }
  2463.       else
  2464.     {
  2465.       char *d = (char *) bfd_alloc (abfd, seclet->size);
  2466.       unsigned int i;
  2467.       boolean ret;
  2468.  
  2469.       for (i = 0; i < seclet->size; i+=2)
  2470.         d[i] = seclet->u.fill.value >> 8;
  2471.       for (i = 1; i < seclet->size; i+=2)
  2472.         d[i] = seclet->u.fill.value;
  2473.       ret = bfd_set_section_contents (abfd, section, d, seclet->offset,
  2474.                       seclet->size);
  2475.       bfd_release (abfd, (PTR) d);
  2476.       return ret;
  2477.     }
  2478.       break;
  2479.  
  2480.     default:
  2481.       abort();
  2482.     }
  2483.  
  2484.   return true;
  2485. }
  2486.  
  2487. /* Add a string to the debugging information we are accumulating for a
  2488.    file.  Return the offset from the fdr string base or from the
  2489.    external string base.  */
  2490.  
  2491. static long
  2492. ecoff_add_string (output_bfd, fdr, string, external)
  2493.      bfd *output_bfd;
  2494.      FDR *fdr;
  2495.      CONST char *string;
  2496.      boolean external;
  2497. {
  2498.   HDRR *symhdr;
  2499.   size_t len;
  2500.   long ret;
  2501.  
  2502.   symhdr = &ecoff_data (output_bfd)->symbolic_header;
  2503.   len = strlen (string);
  2504.   if (external)
  2505.     {
  2506.       strcpy (ecoff_data (output_bfd)->ssext + symhdr->issExtMax, string);
  2507.       ret = symhdr->issExtMax;
  2508.       symhdr->issExtMax += len + 1;
  2509.     }
  2510.   else
  2511.     {
  2512.       strcpy (ecoff_data (output_bfd)->ss + symhdr->issMax, string);
  2513.       ret = fdr->cbSs;
  2514.       symhdr->issMax += len + 1;
  2515.       fdr->cbSs += len + 1;
  2516.     }
  2517.   return ret;
  2518. }
  2519.  
  2520. /* Accumulate the debugging information from an input section.  */
  2521.  
  2522. static boolean
  2523. ecoff_get_debug (output_bfd, seclet, section, relocateable)
  2524.      bfd *output_bfd;
  2525.      bfd_seclet_type *seclet;
  2526.      asection *section;
  2527.      boolean relocateable;
  2528. {
  2529.   bfd *input_bfd;
  2530.   HDRR *output_symhdr;
  2531.   HDRR *input_symhdr;
  2532.   ecoff_data_type *output_ecoff;
  2533.   ecoff_data_type *input_ecoff;
  2534.   unsigned int count;
  2535.   struct sym_ext *sym_out;
  2536.   ecoff_symbol_type *esym_ptr;
  2537.   ecoff_symbol_type *esym_end;
  2538.   unsigned long pdr_off;
  2539.   FDR *fdr_ptr;
  2540.   FDR *fdr_end;
  2541.   struct fdr_ext *fdr_out;
  2542.  
  2543.   input_bfd = seclet->u.indirect.section->owner;
  2544.  
  2545.   /* Don't get the information more than once. */
  2546.   if (input_bfd->output_has_begun)
  2547.     return true;
  2548.   input_bfd->output_has_begun = true;
  2549.  
  2550.   output_ecoff = ecoff_data (output_bfd);
  2551.   output_symhdr = &output_ecoff->symbolic_header;
  2552.  
  2553.   if (input_bfd->xvec->flavour != bfd_target_ecoff_flavour)
  2554.     {
  2555.       FDR fdr;
  2556.       asymbol **symbols;
  2557.       asymbol **sym_ptr;
  2558.       asymbol **sym_end;
  2559.  
  2560.       /* This is not an ECOFF BFD.  Just gather the symbols.  */
  2561.  
  2562.       memset (&fdr, 0, sizeof fdr);
  2563.  
  2564.       fdr.adr = bfd_get_section_vma (output_bfd, section) + seclet->offset;
  2565.       fdr.issBase = output_symhdr->issMax;
  2566.       fdr.cbSs = 0;
  2567.       fdr.rss = ecoff_add_string (output_bfd,
  2568.                   &fdr,
  2569.                   bfd_get_filename (input_bfd),
  2570.                   false);
  2571.       fdr.isymBase = output_symhdr->isymMax;
  2572.  
  2573.       /* Get the local symbols from the input BFD.  */
  2574.       symbols = (asymbol **) bfd_alloc (output_bfd,
  2575.                     get_symtab_upper_bound (input_bfd));
  2576.       if (symbols == (asymbol **) NULL)
  2577.     {
  2578.       bfd_error = no_memory;
  2579.       return false;
  2580.     }
  2581.       sym_end = symbols + bfd_canonicalize_symtab (input_bfd, symbols);
  2582.  
  2583.       /* Handle the local symbols.  Any external symbols are handled
  2584.      separately.  */
  2585.       fdr.csym = 0;
  2586.       for (sym_ptr = symbols; sym_ptr != sym_end; sym_ptr++)
  2587.     {
  2588.       SYMR internal_sym;
  2589.  
  2590.       if (((*sym_ptr)->flags & BSF_EXPORT) != 0)
  2591.         continue;
  2592.       memset (&internal_sym, 0, sizeof internal_sym);
  2593.       internal_sym.iss = ecoff_add_string (output_bfd,
  2594.                            &fdr,
  2595.                            (*sym_ptr)->name,
  2596.                            false);
  2597.  
  2598.       if (bfd_is_com_section ((*sym_ptr)->section)
  2599.           || (*sym_ptr)->section == &bfd_und_section)
  2600.         internal_sym.value = (*sym_ptr)->value;
  2601.       else
  2602.         internal_sym.value = ((*sym_ptr)->value
  2603.                   + (*sym_ptr)->section->output_offset
  2604.                   + (*sym_ptr)->section->output_section->vma);
  2605.       internal_sym.st = stNil;
  2606.       internal_sym.sc = scUndefined;
  2607.       internal_sym.index = indexNil;
  2608.       ecoff_swap_sym_out (output_bfd, &internal_sym,
  2609.                   (output_ecoff->external_sym
  2610.                    + output_symhdr->isymMax));
  2611.       ++fdr.csym;
  2612.       ++output_symhdr->isymMax;
  2613.     }
  2614.  
  2615.       bfd_release (output_bfd, (PTR) symbols);
  2616.  
  2617.       /* Leave everything else in the FDR zeroed out.  This will cause
  2618.      the lang field to be langC.  The fBigendian field will
  2619.      indicate little endian format, but it doesn't matter because
  2620.      it only applies to aux fields and there are none.  */
  2621.  
  2622.       ecoff_swap_fdr_out (output_bfd, &fdr,
  2623.               (output_ecoff->external_fdr
  2624.                + output_symhdr->ifdMax));
  2625.       ++output_symhdr->ifdMax;
  2626.       return true;
  2627.     }
  2628.  
  2629.   /* This is an ECOFF BFD.  We want to grab the information from
  2630.      input_bfd and attach it to output_bfd.  */
  2631.   count = bfd_get_symcount (input_bfd);
  2632.   if (count == 0)
  2633.     return true;
  2634.   input_ecoff = ecoff_data (input_bfd);
  2635.   input_symhdr = &input_ecoff->symbolic_header;
  2636.  
  2637.   /* I think that it is more efficient to simply copy the debugging
  2638.      information from the input BFD to the output BFD.  Because ECOFF
  2639.      uses relative pointers for most of the debugging information,
  2640.      only a little of it has to be changed at all.  */
  2641.  
  2642.   /* Swap in the local symbols, adjust their values, and swap them out
  2643.      again.  The external symbols are handled separately.  */
  2644.   sym_out = output_ecoff->external_sym + output_symhdr->isymMax;
  2645.  
  2646.   esym_ptr = ecoff_data (input_bfd)->canonical_symbols;
  2647.   esym_end = esym_ptr + count;
  2648.   for (; esym_ptr < esym_end; esym_ptr++)
  2649.     {
  2650.       if (esym_ptr->local)
  2651.     {
  2652.       SYMR sym;
  2653.  
  2654.       ecoff_swap_sym_in (input_bfd, esym_ptr->native.lnative, &sym);
  2655.  
  2656.       /* If we're producing an executable, move common symbols
  2657.          into bss.  */
  2658.       if (relocateable == false)
  2659.         {
  2660.           if (sym.sc == scCommon)
  2661.         sym.sc = scBss;
  2662.           else if (sym.sc == scSCommon)
  2663.         sym.sc = scSBss;
  2664.         }
  2665.  
  2666.       if (! bfd_is_com_section (esym_ptr->symbol.section)
  2667.           && (esym_ptr->symbol.flags & BSF_DEBUGGING) == 0
  2668.           && esym_ptr->symbol.section != &bfd_und_section)
  2669.         sym.value = (esym_ptr->symbol.value
  2670.              + esym_ptr->symbol.section->output_offset
  2671.              + esym_ptr->symbol.section->output_section->vma);
  2672.       ecoff_swap_sym_out (output_bfd, &sym, sym_out);
  2673.       ++sym_out;
  2674.     }
  2675.     }
  2676.  
  2677.   /* That should have accounted for all the local symbols in
  2678.      input_bfd.  */
  2679.   BFD_ASSERT ((sym_out - output_ecoff->external_sym) - output_symhdr->isymMax
  2680.           == input_symhdr->isymMax);
  2681.  
  2682.   /* Copy the information that does not need swapping.  */
  2683.   memcpy (output_ecoff->line + output_symhdr->cbLine,
  2684.       input_ecoff->line,
  2685.       input_symhdr->cbLine * sizeof (unsigned char));
  2686.   memcpy (output_ecoff->external_aux + output_symhdr->iauxMax,
  2687.       input_ecoff->external_aux,
  2688.       input_symhdr->iauxMax * sizeof (union aux_ext));
  2689.   memcpy (output_ecoff->ss + output_symhdr->issMax,
  2690.       input_ecoff->ss,
  2691.       input_symhdr->issMax * sizeof (char));
  2692.  
  2693.   /* Some of the information may need to be swapped.  */
  2694.   if (output_bfd->xvec->header_byteorder_big_p
  2695.       == input_bfd->xvec->header_byteorder_big_p)
  2696.     {
  2697.       /* The two BFD's have the same endianness, so memcpy will
  2698.      suffice.  */
  2699.       memcpy (output_ecoff->external_dnr + output_symhdr->idnMax,
  2700.           input_ecoff->external_dnr,
  2701.           input_symhdr->idnMax * sizeof (struct dnr_ext));
  2702.       memcpy (output_ecoff->external_pdr + output_symhdr->ipdMax,
  2703.           input_ecoff->external_pdr,
  2704.           input_symhdr->ipdMax * sizeof (struct pdr_ext));
  2705.       if (input_symhdr->ipdMax == 0)
  2706.     pdr_off = 0;
  2707.       else
  2708.     {
  2709.       PDR pdr;
  2710.  
  2711.       ecoff_swap_pdr_in (input_bfd, input_ecoff->external_pdr, &pdr);
  2712.       pdr_off = pdr.adr;
  2713.     }
  2714.       memcpy (output_ecoff->external_opt + output_symhdr->ioptMax,
  2715.           input_ecoff->external_opt,
  2716.           input_symhdr->ioptMax * sizeof (struct opt_ext));
  2717.     }
  2718.   else
  2719.     {
  2720.       struct dnr_ext *dnr_in;
  2721.       struct dnr_ext *dnr_end;
  2722.       struct dnr_ext *dnr_out;
  2723.       struct pdr_ext *pdr_in;
  2724.       struct pdr_ext *pdr_end;
  2725.       struct pdr_ext *pdr_out;
  2726.       int first_pdr;
  2727.       struct opt_ext *opt_in;
  2728.       struct opt_ext *opt_end;
  2729.       struct opt_ext *opt_out;
  2730.  
  2731.       /* The two BFD's have different endianness, so we must swap
  2732.      everything in and out.  This code would always work, but it
  2733.      would be slow in the normal case.  */
  2734.       dnr_in = input_ecoff->external_dnr;
  2735.       dnr_end = dnr_in + input_symhdr->idnMax;
  2736.       dnr_out = output_ecoff->external_dnr + output_symhdr->idnMax;
  2737.       for (; dnr_in < dnr_end; dnr_in++, dnr_out++)
  2738.     {
  2739.       DNR dnr;
  2740.  
  2741.       ecoff_swap_dnr_in (input_bfd, dnr_in, &dnr);
  2742.       ecoff_swap_dnr_out (output_bfd, &dnr, dnr_out);
  2743.     }
  2744.       pdr_in = input_ecoff->external_pdr;
  2745.       pdr_end = pdr_in + input_symhdr->ipdMax;
  2746.       pdr_out = output_ecoff->external_pdr + output_symhdr->ipdMax;
  2747.       first_pdr = 1;
  2748.       pdr_off = 0;
  2749.       for (; pdr_in < pdr_end; pdr_in++, pdr_out++)
  2750.     {
  2751.       PDR pdr;
  2752.  
  2753.       ecoff_swap_pdr_in (input_bfd, pdr_in, &pdr);
  2754.       ecoff_swap_pdr_out (output_bfd, &pdr, pdr_out);
  2755.       if (first_pdr)
  2756.         {
  2757.           pdr_off = pdr.adr;
  2758.           first_pdr = 0;
  2759.         }
  2760.     }
  2761.       opt_in = input_ecoff->external_opt;
  2762.       opt_end = opt_in + input_symhdr->ioptMax;
  2763.       opt_out = output_ecoff->external_opt + output_symhdr->ioptMax;
  2764.       for (; opt_in < opt_end; opt_in++, opt_out++)
  2765.     {
  2766.       OPTR opt;
  2767.  
  2768.       ecoff_swap_opt_in (input_bfd, opt_in, &opt);
  2769.       ecoff_swap_opt_out (output_bfd, &opt, opt_out);
  2770.     }
  2771.     }
  2772.  
  2773.   /* Set ifdbase so that the external symbols know how to adjust their
  2774.      ifd values.  */
  2775.   input_ecoff->ifdbase = output_symhdr->ifdMax;
  2776.  
  2777.   fdr_ptr = input_ecoff->fdr;
  2778.   fdr_end = fdr_ptr + input_symhdr->ifdMax;
  2779.   fdr_out = output_ecoff->external_fdr + output_symhdr->ifdMax;
  2780.   for (; fdr_ptr < fdr_end; fdr_ptr++, fdr_out++)
  2781.     {
  2782.       FDR fdr;
  2783.  
  2784.       fdr = *fdr_ptr;
  2785.  
  2786.       /* The memory address for this fdr is the address for the seclet
  2787.      plus the offset to this fdr within input_bfd.  For some
  2788.      reason the offset of the first procedure pointer is also
  2789.      added in.  */
  2790.       fdr.adr = (bfd_get_section_vma (output_bfd, section)
  2791.          + seclet->offset
  2792.          + (fdr_ptr->adr - input_ecoff->fdr->adr)
  2793.          + pdr_off);
  2794.  
  2795.       fdr.issBase += output_symhdr->issMax;
  2796.       fdr.isymBase += output_symhdr->isymMax;
  2797.       fdr.ilineBase += output_symhdr->ilineMax;
  2798.       fdr.ioptBase += output_symhdr->ioptMax;
  2799.       fdr.ipdFirst += output_symhdr->ipdMax;
  2800.       fdr.iauxBase += output_symhdr->iauxMax;
  2801.       fdr.rfdBase += output_symhdr->crfd;
  2802.  
  2803.       /* If there are no RFD's, we are going to add some.  We don't
  2804.      want to adjust irfd for this, so that all the FDR's can share
  2805.      the RFD's.  */
  2806.       if (input_symhdr->crfd == 0)
  2807.     fdr.crfd = input_symhdr->ifdMax;
  2808.  
  2809.       if (fdr.cbLine != 0)
  2810.     fdr.cbLineOffset += output_symhdr->cbLine;
  2811.  
  2812.       ecoff_swap_fdr_out (output_bfd, &fdr, fdr_out);
  2813.     }
  2814.  
  2815.   if (input_symhdr->crfd > 0)
  2816.     {
  2817.       struct rfd_ext *rfd_in;
  2818.       struct rfd_ext *rfd_end;
  2819.       struct rfd_ext *rfd_out;
  2820.  
  2821.       /* Swap and adjust the RFD's.  RFD's are only created by the
  2822.      linker, so this will only be necessary if one of the input
  2823.      files is the result of a partial link.  Presumably all
  2824.      necessary RFD's are present.  */
  2825.       rfd_in = input_ecoff->external_rfd;
  2826.       rfd_end = rfd_in + input_symhdr->crfd;
  2827.       rfd_out = output_ecoff->external_rfd + output_symhdr->crfd;
  2828.       for (; rfd_in < rfd_end; rfd_in++, rfd_out++)
  2829.     {
  2830.       RFDT rfd;
  2831.  
  2832.       ecoff_swap_rfd_in (input_bfd, rfd_in, &rfd);
  2833.       rfd += output_symhdr->ifdMax;
  2834.       ecoff_swap_rfd_out (output_bfd, &rfd, rfd_out);
  2835.     }
  2836.       output_symhdr->crfd += input_symhdr->crfd;
  2837.     }
  2838.   else
  2839.     {
  2840.       struct rfd_ext *rfd_out;
  2841.       struct rfd_ext *rfd_end;
  2842.       RFDT rfd;
  2843.  
  2844.       /* Create RFD's.  Some of the debugging information includes
  2845.      relative file indices.  These indices are taken as indices to
  2846.      the RFD table if there is one, or to the global table if
  2847.      there is not.  If we did not create RFD's, we would have to
  2848.      parse and adjust all the debugging information which contains
  2849.      file indices.  */
  2850.       rfd = output_symhdr->ifdMax;
  2851.       rfd_out = output_ecoff->external_rfd + output_symhdr->crfd;
  2852.       rfd_end = rfd_out + input_symhdr->ifdMax;
  2853.       for (; rfd_out < rfd_end; rfd_out++, rfd++)
  2854.     ecoff_swap_rfd_out (output_bfd, &rfd, rfd_out);
  2855.       output_symhdr->crfd += input_symhdr->ifdMax;
  2856.     }
  2857.  
  2858.   /* Combine the register masks.  */
  2859.   {
  2860.     int i;
  2861.  
  2862.     output_ecoff->gprmask |= input_ecoff->gprmask;
  2863.     for (i = 0; i < 4; i++)
  2864.       output_ecoff->cprmask[i] |= input_ecoff->cprmask[i];
  2865.   }
  2866.  
  2867.   /* Update the counts.  */
  2868.   output_symhdr->ilineMax += input_symhdr->ilineMax;
  2869.   output_symhdr->cbLine += input_symhdr->cbLine;
  2870.   output_symhdr->idnMax += input_symhdr->idnMax;
  2871.   output_symhdr->ipdMax += input_symhdr->ipdMax;
  2872.   output_symhdr->isymMax += input_symhdr->isymMax;
  2873.   output_symhdr->ioptMax += input_symhdr->ioptMax;
  2874.   output_symhdr->iauxMax += input_symhdr->iauxMax;
  2875.   output_symhdr->issMax += input_symhdr->issMax;
  2876.   output_symhdr->ifdMax += input_symhdr->ifdMax;
  2877.  
  2878.   return true;
  2879. }
  2880.  
  2881. /* This is the actual link routine.  It makes two passes over all the
  2882.    seclets.  */
  2883.  
  2884. static boolean
  2885. ecoff_bfd_seclet_link (abfd, data, relocateable)
  2886.      bfd *abfd;
  2887.      PTR data;
  2888.      boolean relocateable;
  2889. {
  2890.   HDRR *symhdr;
  2891.   int ipass;
  2892.   register asection *o;
  2893.   register bfd_seclet_type *p;
  2894.   asymbol **sym_ptr_ptr;
  2895.   bfd_size_type size;
  2896.   char *raw;
  2897.  
  2898.   /* We accumulate the debugging information counts in the symbolic
  2899.      header.  */
  2900.   symhdr = &ecoff_data (abfd)->symbolic_header;
  2901.   symhdr->magic = magicSym;
  2902.   /* FIXME: What should the version stamp be?  */
  2903.   symhdr->vstamp = 0;
  2904.   symhdr->ilineMax = 0;
  2905.   symhdr->cbLine = 0;
  2906.   symhdr->idnMax = 0;
  2907.   symhdr->ipdMax = 0;
  2908.   symhdr->isymMax = 0;
  2909.   symhdr->ioptMax = 0;
  2910.   symhdr->iauxMax = 0;
  2911.   symhdr->issMax = 0;
  2912.   symhdr->issExtMax = 0;
  2913.   symhdr->ifdMax = 0;
  2914.   symhdr->crfd = 0;
  2915.   symhdr->iextMax = 0;
  2916.  
  2917.   /* We need to copy over the debugging symbols from each input BFD.
  2918.      When we do this copying, we have to adjust the text address in
  2919.      the FDR structures, so we have to know the text address used for
  2920.      the input BFD.  Since we only want to copy the symbols once per
  2921.      input BFD, but we are going to look at each input BFD multiple
  2922.      times (once for each section it provides), we arrange to always
  2923.      look at the text section first.  That means that when we copy the
  2924.      debugging information, we always know the text address.  So we
  2925.      actually do each pass in two sub passes; first the text sections,
  2926.      then the non-text sections.  We use the output_has_begun flag to
  2927.      determine whether we have copied over the debugging information
  2928.      yet.  */
  2929.  
  2930.   /* Do the first pass: set the output section contents and count the
  2931.      debugging information.  */
  2932.   ecoff_clear_output_flags (abfd);
  2933.   for (ipass = 0; ipass < 2; ipass++)
  2934.     {
  2935.       for (o = abfd->sections; o != (asection *) NULL; o = o->next)
  2936.     {
  2937.       /* For SEC_CODE sections, (flags & SEC_CODE) == 0 is false,
  2938.          so they are done on pass 0.  For other sections the
  2939.          expression is true, so they are done on pass 1.  */
  2940.       if (((o->flags & SEC_CODE) == 0) != ipass)
  2941.         continue;
  2942.  
  2943.       for (p = o->seclets_head;
  2944.            p != (bfd_seclet_type *) NULL;
  2945.            p = p->next)
  2946.         {
  2947.           if (ecoff_dump_seclet (abfd, p, o, data, relocateable)
  2948.           == false)
  2949.         return false;
  2950.         }
  2951.     }
  2952.     }
  2953.  
  2954.   /* We handle the external symbols differently.  We use the ones
  2955.      attached to the output_bfd.  The linker will have already
  2956.      determined which symbols are to be attached.  Here we just
  2957.      determine how much space we will need for them.  */
  2958.   sym_ptr_ptr = bfd_get_outsymbols (abfd);
  2959.   if (sym_ptr_ptr != NULL)
  2960.     {
  2961.       asymbol **sym_end;
  2962.  
  2963.       sym_end = sym_ptr_ptr + bfd_get_symcount (abfd);
  2964.       for (; sym_ptr_ptr < sym_end; sym_ptr_ptr++)
  2965.     {
  2966.       if (((*sym_ptr_ptr)->flags & BSF_DEBUGGING) == 0
  2967.           && ((*sym_ptr_ptr)->flags & BSF_LOCAL) == 0)
  2968.         {
  2969.           ++symhdr->iextMax;
  2970.           symhdr->issExtMax += strlen ((*sym_ptr_ptr)->name) + 1;
  2971.         }
  2972.     }
  2973.     }
  2974.  
  2975.   /* Adjust the counts so that structures are longword aligned.  */
  2976.   symhdr->cbLine = (symhdr->cbLine + 3) &~ 3;
  2977.   symhdr->issMax = (symhdr->issMax + 3) &~ 3;
  2978.   symhdr->issExtMax = (symhdr->issExtMax + 3) &~ 3;
  2979.  
  2980.   /* Now the counts in symhdr are the correct size for the debugging
  2981.      information.  We allocate the right amount of space, and reset
  2982.      the counts so that the second pass can use them as indices.  It
  2983.      would be possible to output the debugging information directly to
  2984.      the file in pass 2, rather than to build it in memory and then
  2985.      write it out.  Outputting to the file would require a lot of
  2986.      seeks and small writes, though, and I think this approach is
  2987.      faster.  */
  2988.   size = (symhdr->cbLine * sizeof (unsigned char)
  2989.       + symhdr->idnMax * sizeof (struct dnr_ext)
  2990.       + symhdr->ipdMax * sizeof (struct pdr_ext)
  2991.       + symhdr->isymMax * sizeof (struct sym_ext)
  2992.       + symhdr->ioptMax * sizeof (struct opt_ext)
  2993.       + symhdr->iauxMax * sizeof (union aux_ext)
  2994.       + symhdr->issMax * sizeof (char)
  2995.       + symhdr->issExtMax * sizeof (char)
  2996.       + symhdr->ifdMax * sizeof (struct fdr_ext)
  2997.       + symhdr->crfd * sizeof (struct rfd_ext)
  2998.       + symhdr->iextMax * sizeof (struct ext_ext));
  2999.   raw = (char *) bfd_alloc (abfd, size);
  3000.   if (raw == (char *) NULL)
  3001.     {
  3002.       bfd_error = no_memory;
  3003.       return false;
  3004.     }
  3005.   ecoff_data (abfd)->raw_size = size;
  3006.   ecoff_data (abfd)->raw_syments = (PTR) raw;
  3007.  
  3008.   /* Initialize the raw pointers.  */
  3009. #define SET(field, count, type) \
  3010.   ecoff_data (abfd)->field = (type *) raw; \
  3011.   raw += symhdr->count * sizeof (type)
  3012.  
  3013.   SET (line, cbLine, unsigned char);
  3014.   SET (external_dnr, idnMax, struct dnr_ext);
  3015.   SET (external_pdr, ipdMax, struct pdr_ext);
  3016.   SET (external_sym, isymMax, struct sym_ext);
  3017.   SET (external_opt, ioptMax, struct opt_ext);
  3018.   SET (external_aux, iauxMax, union aux_ext);
  3019.   SET (ss, issMax, char);
  3020.   SET (ssext, issExtMax, char);
  3021.   SET (external_fdr, ifdMax, struct fdr_ext);
  3022.   SET (external_rfd, crfd, struct rfd_ext);
  3023.   SET (external_ext, iextMax, struct ext_ext);
  3024. #undef SET
  3025.  
  3026.   /* Reset the counts so the second pass can use them to know how far
  3027.      it has gotten.  */
  3028.   symhdr->ilineMax = 0;
  3029.   symhdr->cbLine = 0;
  3030.   symhdr->idnMax = 0;
  3031.   symhdr->ipdMax = 0;
  3032.   symhdr->isymMax = 0;
  3033.   symhdr->ioptMax = 0;
  3034.   symhdr->iauxMax = 0;
  3035.   symhdr->issMax = 0;
  3036.   symhdr->issExtMax = 0;
  3037.   symhdr->ifdMax = 0;
  3038.   symhdr->crfd = 0;
  3039.   symhdr->iextMax = 0;
  3040.  
  3041.   /* Do the second pass: accumulate the debugging information.  */
  3042.   ecoff_clear_output_flags (abfd);
  3043.   for (ipass = 0; ipass < 2; ipass++)
  3044.     {
  3045.       for (o = abfd->sections; o != (asection *) NULL; o = o->next)
  3046.     {
  3047.       if (((o->flags & SEC_CODE) == 0) != ipass)
  3048.         continue;
  3049.       for (p = o->seclets_head;
  3050.            p != (bfd_seclet_type *) NULL;
  3051.            p = p->next)
  3052.         {
  3053.           if (p->type == bfd_indirect_seclet)
  3054.         {
  3055.           if (ecoff_get_debug (abfd, p, o, relocateable) == false)
  3056.             return false;
  3057.         }
  3058.         }
  3059.     }
  3060.     }
  3061.  
  3062.   /* Put in the external symbols.  */
  3063.   sym_ptr_ptr = bfd_get_outsymbols (abfd);
  3064.   if (sym_ptr_ptr != NULL)
  3065.     {
  3066.       char *ssext;
  3067.       struct ext_ext *external_ext;
  3068.  
  3069.       ssext = ecoff_data (abfd)->ssext;
  3070.       external_ext = ecoff_data (abfd)->external_ext;
  3071.       for (; *sym_ptr_ptr != NULL; sym_ptr_ptr++)
  3072.     {
  3073.       asymbol *sym_ptr;
  3074.       EXTR esym;
  3075.  
  3076.       sym_ptr = *sym_ptr_ptr;
  3077.  
  3078.       if ((sym_ptr->flags & BSF_DEBUGGING) != 0
  3079.           || (sym_ptr->flags & BSF_LOCAL) != 0)
  3080.         continue;
  3081.  
  3082.       /* The enative pointer can be NULL for a symbol created by
  3083.          the linker via ecoff_make_empty_symbol.  */
  3084.       if (bfd_asymbol_flavour (sym_ptr) != bfd_target_ecoff_flavour
  3085.           || (((ecoff_symbol_type *) sym_ptr)->native.enative
  3086.           == (struct ext_ext *) NULL))
  3087.         {
  3088.           esym.jmptbl = 0;
  3089.           esym.cobol_main = 0;
  3090.           esym.weakext = 0;
  3091.           esym.reserved = 0;
  3092.           esym.ifd = ifdNil;
  3093.           /* FIXME: we can do better than this for st and sc.  */
  3094.           esym.asym.st = stGlobal;
  3095.           esym.asym.sc = scAbs;
  3096.           esym.asym.reserved = 0;
  3097.           esym.asym.index = indexNil;
  3098.         }
  3099.       else
  3100.         {
  3101.           ecoff_symbol_type *ecoff_sym_ptr;
  3102.  
  3103.           ecoff_sym_ptr = (ecoff_symbol_type *) sym_ptr;
  3104.           if (ecoff_sym_ptr->local)
  3105.         abort ();
  3106.           ecoff_swap_ext_in (abfd, ecoff_sym_ptr->native.enative, &esym);
  3107.  
  3108.           /* If we're producing an executable, move common symbols
  3109.          into bss.  */
  3110.           if (relocateable == false)
  3111.         {
  3112.           if (esym.asym.sc == scCommon)
  3113.             esym.asym.sc = scBss;
  3114.           else if (esym.asym.sc == scSCommon)
  3115.             esym.asym.sc = scSBss;
  3116.         }
  3117.  
  3118.           /* Adjust the FDR index for the symbol by that used for
  3119.          the input BFD.  */
  3120.           esym.ifd += ecoff_data (bfd_asymbol_bfd (sym_ptr))->ifdbase;
  3121.         }
  3122.  
  3123.       esym.asym.iss = symhdr->issExtMax;
  3124.  
  3125.       if (bfd_is_com_section (sym_ptr->section)
  3126.           || sym_ptr->section == &bfd_und_section)
  3127.         esym.asym.value = sym_ptr->value;
  3128.       else
  3129.         esym.asym.value = (sym_ptr->value
  3130.                    + sym_ptr->section->output_offset
  3131.                    + sym_ptr->section->output_section->vma);
  3132.  
  3133.       ecoff_swap_ext_out (abfd, &esym, external_ext + symhdr->iextMax);
  3134.  
  3135.       ecoff_set_sym_index (sym_ptr, symhdr->iextMax);
  3136.  
  3137.       ++symhdr->iextMax;
  3138.  
  3139.       strcpy (ssext + symhdr->issExtMax, sym_ptr->name);
  3140.       symhdr->issExtMax += strlen (sym_ptr->name) + 1;
  3141.     }
  3142.     }
  3143.  
  3144.   /* Adjust the counts so that structures are longword aligned.  */
  3145.   symhdr->cbLine = (symhdr->cbLine + 3) &~ 3;
  3146.   symhdr->issMax = (symhdr->issMax + 3) &~ 3;
  3147.   symhdr->issExtMax = (symhdr->issExtMax + 3) &~ 3;
  3148.  
  3149.   return true;
  3150. }
  3151.  
  3152. /* Set the architecture.  The only architecture we support here is
  3153.    mips.  We set the architecture anyhow, since many callers ignore
  3154.    the return value.  */
  3155.  
  3156. static boolean
  3157. ecoff_set_arch_mach (abfd, arch, machine)
  3158.      bfd *abfd;
  3159.      enum bfd_architecture arch;
  3160.      unsigned long machine;
  3161. {
  3162.   bfd_default_set_arch_mach (abfd, arch, machine);
  3163.   return arch == bfd_arch_mips;
  3164. }
  3165.  
  3166. /* Get the size of the section headers.  We do not output the .scommon
  3167.    section which we created in ecoff_mkobject.  */
  3168.  
  3169. static int
  3170. ecoff_sizeof_headers (abfd, reloc)
  3171.      bfd *abfd;
  3172.      boolean reloc;
  3173. {
  3174.   return FILHSZ + AOUTSZ + (abfd->section_count - 1) * SCNHSZ;
  3175. }
  3176.  
  3177. /* Calculate the file position for each section, and set
  3178.    reloc_filepos.  */
  3179.  
  3180. static void
  3181. ecoff_compute_section_file_positions (abfd)
  3182.      bfd *abfd;
  3183. {
  3184.   asection *current;
  3185.   file_ptr sofar;
  3186.   file_ptr old_sofar;
  3187.   boolean first_data;
  3188.  
  3189.   if (bfd_get_start_address (abfd)) 
  3190.     abfd->flags |= EXEC_P;
  3191.  
  3192.   sofar = ecoff_sizeof_headers (abfd, false);
  3193.  
  3194.   first_data = true;
  3195.   for (current = abfd->sections;
  3196.        current != (asection *) NULL;
  3197.        current = current->next)
  3198.     {
  3199.       /* Only deal with sections which have contents */
  3200.       if (! (current->flags & SEC_HAS_CONTENTS)
  3201.       || strcmp (current->name, SCOMMON) == 0)
  3202.     continue;
  3203.  
  3204.       /* On Ultrix, the data sections in an executable file must be
  3205.      aligned to a page boundary within the file.  This does not
  3206.      affect the section size, though.  FIXME: Does this work for
  3207.      other platforms?  */
  3208.       if ((abfd->flags & EXEC_P) != 0
  3209.       && first_data != false
  3210.       && (current->flags & SEC_CODE) == 0)
  3211.     {
  3212.       sofar = (sofar + ROUND_SIZE - 1) &~ (ROUND_SIZE - 1);
  3213.       first_data = false;
  3214.     }
  3215.  
  3216.       /* Align the sections in the file to the same boundary on
  3217.      which they are aligned in virtual memory.  */
  3218.       old_sofar = sofar;
  3219.       sofar = BFD_ALIGN (sofar, 1 << current->alignment_power);
  3220.  
  3221.       current->filepos = sofar;
  3222.  
  3223.       sofar += current->_raw_size;
  3224.  
  3225.       /* make sure that this section is of the right size too */
  3226.       old_sofar = sofar;
  3227.       sofar = BFD_ALIGN (sofar, 1 << current->alignment_power);
  3228.       current->_raw_size += sofar - old_sofar;
  3229.     }
  3230.  
  3231.   ecoff_data (abfd)->reloc_filepos = sofar;
  3232. }
  3233.  
  3234. /* Set the contents of a section.  */
  3235.  
  3236. static boolean
  3237. ecoff_set_section_contents (abfd, section, location, offset, count)
  3238.      bfd *abfd;
  3239.      asection *section;
  3240.      PTR location;
  3241.      file_ptr offset;
  3242.      bfd_size_type count;
  3243. {
  3244.   if (abfd->output_has_begun == false)
  3245.     ecoff_compute_section_file_positions (abfd);
  3246.  
  3247.   bfd_seek (abfd, (file_ptr) (section->filepos + offset), SEEK_SET);
  3248.  
  3249.   if (count != 0)
  3250.     return (bfd_write (location, 1, count, abfd) == count) ? true : false;
  3251.  
  3252.   return true;
  3253. }
  3254.  
  3255. /* Write out an ECOFF file.  */
  3256.  
  3257. static boolean
  3258. ecoff_write_object_contents (abfd)
  3259.      bfd *abfd;
  3260. {
  3261.   asection *current;
  3262.   unsigned int count;
  3263.   file_ptr scn_base;
  3264.   file_ptr reloc_base;
  3265.   file_ptr sym_base;
  3266.   unsigned long reloc_size;
  3267.   unsigned long text_size;
  3268.   unsigned long text_start;
  3269.   unsigned long data_size;
  3270.   unsigned long data_start;
  3271.   unsigned long bss_size;
  3272.   struct internal_filehdr internal_f;
  3273.   struct internal_aouthdr internal_a;
  3274.   int i;
  3275.  
  3276.   bfd_error = system_call_error;
  3277.  
  3278.   if(abfd->output_has_begun == false)
  3279.     ecoff_compute_section_file_positions(abfd);
  3280.  
  3281.   if (abfd->sections != (asection *) NULL)
  3282.     scn_base = abfd->sections->filepos;
  3283.   else
  3284.     scn_base = 0;
  3285.   reloc_base = ecoff_data (abfd)->reloc_filepos;
  3286.  
  3287.   count = 1;
  3288.   reloc_size = 0;
  3289.   for (current = abfd->sections;
  3290.        current != (asection *)NULL; 
  3291.        current = current->next) 
  3292.     {
  3293.       if (strcmp (current->name, SCOMMON) == 0)
  3294.     continue;
  3295.       current->target_index = count;
  3296.       ++count;
  3297.       if (current->reloc_count != 0)
  3298.     {
  3299.       bfd_size_type relsize;
  3300.  
  3301.       current->rel_filepos = reloc_base;
  3302.       relsize = current->reloc_count * RELSZ;
  3303.       reloc_size += relsize;
  3304.       reloc_base += relsize;
  3305.     }
  3306.       else
  3307.     current->rel_filepos = 0;
  3308.     }
  3309.  
  3310.   sym_base = reloc_base + reloc_size;
  3311.  
  3312.   /* At least on Ultrix, the symbol table of an executable file must
  3313.      be aligned to a page boundary.  FIXME: Is this true on other
  3314.      platforms?  */
  3315.   if ((abfd->flags & EXEC_P) != 0)
  3316.     sym_base = (sym_base + ROUND_SIZE - 1) &~ (ROUND_SIZE - 1);
  3317.  
  3318.   ecoff_data (abfd)->sym_filepos = sym_base;
  3319.  
  3320.   text_size = ecoff_sizeof_headers (abfd, false);
  3321.   text_start = 0;
  3322.   data_size = 0;
  3323.   data_start = 0;
  3324.   bss_size = 0;
  3325.  
  3326.   /* Write section headers to the file.  */
  3327.  
  3328.   internal_f.f_nscns = 0;
  3329.   if (bfd_seek (abfd, (file_ptr) (FILHSZ + AOUTSZ), SEEK_SET) != 0)
  3330.     return false;
  3331.   for (current = abfd->sections;
  3332.        current != (asection *) NULL;
  3333.        current = current->next)
  3334.     {
  3335.       struct internal_scnhdr section;
  3336.       bfd_vma vma;
  3337.  
  3338.       if (strcmp (current->name, SCOMMON) == 0)
  3339.     {
  3340.       BFD_ASSERT (bfd_get_section_size_before_reloc (current) == 0
  3341.               && current->reloc_count == 0);
  3342.       continue;
  3343.     }
  3344.  
  3345.       ++internal_f.f_nscns;
  3346.  
  3347.       strncpy (section.s_name, current->name, sizeof section.s_name);
  3348.  
  3349.       /* FIXME: is this correct for shared libraries?  I think it is
  3350.      but I have no platform to check.  Ian Lance Taylor.  */
  3351.       vma = bfd_get_section_vma (abfd, current);
  3352.       if (strcmp (current->name, _LIB) == 0)
  3353.     section.s_vaddr = 0;
  3354.       else
  3355.     section.s_vaddr = vma;
  3356.  
  3357.       section.s_paddr = vma;
  3358.       section.s_size = bfd_get_section_size_before_reloc (current);
  3359.  
  3360.       /* If this section has no size or is unloadable then the scnptr
  3361.      will be 0 too.  */
  3362.       if (current->_raw_size == 0
  3363.       || (current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
  3364.     section.s_scnptr = 0;
  3365.       else
  3366.     section.s_scnptr = current->filepos;
  3367.       section.s_relptr = current->rel_filepos;
  3368.  
  3369.       /* FIXME: the lnnoptr of the .sbss or .sdata section of an
  3370.      object file produced by the assembler is supposed to point to
  3371.      information about how much room is required by objects of
  3372.      various different sizes.  I think this only matters if we
  3373.      want the linker to compute the best size to use, or
  3374.      something.  I don't know what happens if the information is
  3375.      not present.  */
  3376.       section.s_lnnoptr = 0;
  3377.  
  3378.       section.s_nreloc = current->reloc_count;
  3379.       section.s_nlnno = 0;
  3380.       section.s_flags = ecoff_sec_to_styp_flags (current->name,
  3381.                          current->flags);
  3382.  
  3383.       {
  3384.     SCNHDR buff;
  3385.  
  3386.     ecoff_swap_scnhdr_out (abfd, (PTR) §ion, (PTR) &buff);
  3387.     if (bfd_write ((PTR) &buff, 1, SCNHSZ, abfd) != SCNHSZ)
  3388.       return false;
  3389.       }
  3390.  
  3391.       if ((section.s_flags & STYP_TEXT) != 0)
  3392.     {
  3393.       text_size += bfd_get_section_size_before_reloc (current);
  3394.       if (text_start == 0 || text_start > vma)
  3395.         text_start = vma;
  3396.     }
  3397.       else if ((section.s_flags & STYP_RDATA) != 0
  3398.            || (section.s_flags & STYP_DATA) != 0
  3399.            || (section.s_flags & STYP_LIT8) != 0
  3400.            || (section.s_flags & STYP_LIT4) != 0
  3401.            || (section.s_flags & STYP_SDATA) != 0)
  3402.     {
  3403.       data_size += bfd_get_section_size_before_reloc (current);
  3404.       if (data_start == 0 || data_start > vma)
  3405.         data_start = vma;
  3406.     }
  3407.       else if ((section.s_flags & STYP_BSS) != 0
  3408.            || (section.s_flags & STYP_SBSS) != 0)
  3409.     bss_size += bfd_get_section_size_before_reloc (current);
  3410.     }    
  3411.  
  3412.   /* Set up the file header.  */
  3413.  
  3414.   if (abfd->xvec->header_byteorder_big_p != false)
  3415.       abort();
  3416.   else
  3417.     internal_f.f_magic = ALPHAMAGIC;
  3418.  
  3419.   /*
  3420.     We will NOT put a fucking timestamp in the header here. Every time you
  3421.     put it back, I will come in and take it out again.  I'm sorry.  This
  3422.     field does not belong here.  We fill it with a 0 so it compares the
  3423.     same but is not a reasonable time. -- gnu@cygnus.com
  3424.     */
  3425.   internal_f.f_timdat = 0;
  3426.  
  3427.   if (bfd_get_symcount (abfd) != 0)
  3428.     {
  3429.       /* The ECOFF f_nsyms field is not actually the number of
  3430.      symbols, it's the size of symbolic information header.  */
  3431.       internal_f.f_nsyms = sizeof (struct hdr_ext);
  3432.       internal_f.f_symptr = sym_base;
  3433.     }
  3434.   else
  3435.     {
  3436.       internal_f.f_nsyms = 0;
  3437.       internal_f.f_symptr = 0;
  3438.     }
  3439.  
  3440.   internal_f.f_opthdr = AOUTSZ;
  3441.  
  3442.   internal_f.f_flags = F_LNNO;
  3443.   if (reloc_size == 0)
  3444.     internal_f.f_flags |= F_RELFLG;
  3445.   if (bfd_get_symcount (abfd) == 0)
  3446.     internal_f.f_flags |= F_LSYMS;
  3447.   if (abfd->flags & EXEC_P)
  3448.     internal_f.f_flags |= F_EXEC;
  3449.  
  3450.   if (! abfd->xvec->byteorder_big_p)
  3451.     internal_f.f_flags |= F_AR32WR;
  3452.   else
  3453.     internal_f.f_flags |= F_AR32W;
  3454.  
  3455.   /* Set up the ``optional'' header.  */
  3456.   internal_a.magic = ZMAGIC;
  3457.  
  3458.   /* FIXME: This is what Ultrix puts in, and it makes the Ultrix
  3459.      linker happy.  But, is it right?  */
  3460.   internal_a.vstamp = 0x20a;
  3461.  
  3462.   /* At least on Ultrix, these have to be rounded to page boundaries.
  3463.      FIXME: Is this true on other platforms?  */
  3464.   internal_a.tsize = (text_size + ROUND_SIZE - 1) &~ (ROUND_SIZE - 1);
  3465.   internal_a.text_start = text_start &~ (ROUND_SIZE - 1);
  3466.   internal_a.dsize = (data_size + ROUND_SIZE - 1) &~ (ROUND_SIZE - 1);
  3467.   internal_a.data_start = data_start &~ (ROUND_SIZE - 1);
  3468.  
  3469.   /* On Ultrix, the initial portions of the .sbss and .bss segments
  3470.      are at the end of the data section.  The bsize field in the
  3471.      optional header records how many bss bytes are required beyond
  3472.      those in the data section.  The value is not rounded to a page
  3473.      boundary.  */
  3474.   if (bss_size < internal_a.dsize - data_size)
  3475.     bss_size = 0;
  3476.   else
  3477.     bss_size -= internal_a.dsize - data_size;
  3478.   internal_a.bsize = bss_size;
  3479.   internal_a.bss_start = internal_a.data_start + internal_a.dsize;
  3480.  
  3481.   internal_a.entry = bfd_get_start_address (abfd);
  3482.  
  3483.   internal_a.gp_value = ecoff_data (abfd)->gp;
  3484.  
  3485.   internal_a.gprmask = ecoff_data (abfd)->gprmask;
  3486.   for (i = 0; i < 4; i++)
  3487.     internal_a.cprmask[i] = ecoff_data (abfd)->cprmask[i];
  3488.  
  3489.   /* Write out the file header and the optional header.  */
  3490.  
  3491.   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
  3492.     return false;
  3493.  
  3494.   {
  3495.     FILHDR buff;
  3496.     ecoff_swap_filehdr_out (abfd, (PTR) &internal_f, (PTR) &buff);
  3497.     if (bfd_write ((PTR) &buff, 1, FILHSZ, abfd) != FILHSZ)
  3498.       return false;
  3499.   }
  3500.  
  3501.   {
  3502.     AOUTHDR buff;
  3503.  
  3504.     ecoff_swap_aouthdr_out (abfd, (PTR) &internal_a, (PTR) &buff);
  3505.     if (bfd_write ((PTR) &buff, 1, AOUTSZ, abfd) != AOUTSZ)
  3506.       return false;
  3507.   }
  3508.  
  3509.   /* Write out the relocs.  */
  3510.   for (current = abfd->sections;
  3511.        current != (asection *) NULL;
  3512.        current = current->next)
  3513.     {
  3514.       RELOC *buff;
  3515.       arelent **reloc_ptr_ptr;
  3516.       arelent **reloc_end;
  3517.       RELOC *out_ptr;
  3518.  
  3519.       if (current->reloc_count == 0)
  3520.     continue;
  3521.  
  3522.       buff = (RELOC *) bfd_alloc (abfd, current->reloc_count * RELSZ);
  3523.       if (buff == (RELOC *) NULL)
  3524.     {
  3525.       bfd_error = no_memory;
  3526.       return false;
  3527.     }
  3528.  
  3529.       reloc_ptr_ptr = current->orelocation;
  3530.       reloc_end = reloc_ptr_ptr + current->reloc_count;
  3531.       out_ptr = buff;
  3532.       for (; reloc_ptr_ptr < reloc_end; reloc_ptr_ptr++, out_ptr++)
  3533.     {
  3534.       arelent *reloc;
  3535.       asymbol *sym;
  3536.       struct internal_reloc in;
  3537.       
  3538.       memset (&in, 0, sizeof in);
  3539.  
  3540.       reloc = *reloc_ptr_ptr;
  3541.       sym = *reloc->sym_ptr_ptr;
  3542.  
  3543.       /* This must be an ECOFF reloc.  */
  3544.       BFD_ASSERT (reloc->howto != (reloc_howto_type *) NULL
  3545.               && reloc->howto >= ecoff_howto_table
  3546.               && (reloc->howto
  3547.               < (ecoff_howto_table + ECOFF_HOWTO_COUNT)));
  3548.  
  3549.       in.r_vaddr = reloc->address + bfd_get_section_vma (abfd, current);
  3550.       in.r_type = reloc->howto->type;
  3551.  
  3552.       /* If this is a REFHI reloc, the next one must be a REFLO
  3553.          reloc for the same symbol.  */
  3554.       BFD_ASSERT (in.r_type != ECOFF_R_REFHI
  3555.               || (reloc_ptr_ptr < reloc_end
  3556.               && (reloc_ptr_ptr[1]->howto
  3557.                   != (reloc_howto_type *) NULL)
  3558.               && (reloc_ptr_ptr[1]->howto->type
  3559.                   == ECOFF_R_REFLO)
  3560.               && (sym == *reloc_ptr_ptr[1]->sym_ptr_ptr)));
  3561.  
  3562.       if ((sym->flags & BSF_SECTION_SYM) == 0)
  3563.         {
  3564.           in.r_symndx = ecoff_get_sym_index (*reloc->sym_ptr_ptr);
  3565.           in.r_extern = 1;
  3566.         }
  3567.       else
  3568.         {
  3569.           CONST char *name;
  3570.  
  3571.           name = bfd_get_section_name (abfd, bfd_get_section (sym));
  3572.           if (strcmp (name, ".text") == 0)
  3573.         in.r_symndx = RELOC_SECTION_TEXT;
  3574.           else if (strcmp (name, ".rdata") == 0)
  3575.         in.r_symndx = RELOC_SECTION_RDATA;
  3576.           else if (strcmp (name, ".data") == 0)
  3577.         in.r_symndx = RELOC_SECTION_DATA;
  3578.           else if (strcmp (name, ".sdata") == 0)
  3579.         in.r_symndx = RELOC_SECTION_SDATA;
  3580.           else if (strcmp (name, ".sbss") == 0)
  3581.         in.r_symndx = RELOC_SECTION_SBSS;
  3582.           else if (strcmp (name, ".bss") == 0)
  3583.         in.r_symndx = RELOC_SECTION_BSS;
  3584.           else if (strcmp (name, ".init") == 0)
  3585.         in.r_symndx = RELOC_SECTION_INIT;
  3586.           else if (strcmp (name, ".lit8") == 0)
  3587.         in.r_symndx = RELOC_SECTION_LIT8;
  3588.           else if (strcmp (name, ".lit4") == 0)
  3589.         in.r_symndx = RELOC_SECTION_LIT4;
  3590.           else
  3591.         abort ();
  3592.           in.r_extern = 0;
  3593.         }
  3594.  
  3595.       ecoff_swap_reloc_out (abfd, (PTR) &in, (PTR) out_ptr);
  3596.     }
  3597.  
  3598.       if (bfd_seek (abfd, current->rel_filepos, SEEK_SET) != 0)
  3599.     return false;
  3600.       if (bfd_write ((PTR) buff, RELSZ, current->reloc_count, abfd)
  3601.       != RELSZ * current->reloc_count)
  3602.     return false;
  3603.       bfd_release (abfd, (PTR) buff);
  3604.     }
  3605.  
  3606.   /* Write out the symbolic debugging information.  */
  3607.   if (bfd_get_symcount (abfd) > 0)
  3608.     {
  3609.       HDRR *symhdr;
  3610.       unsigned long sym_offset;
  3611.       struct hdr_ext buff;
  3612.  
  3613.       /* Set up the offsets in the symbolic header.  */
  3614.       symhdr = &ecoff_data (abfd)->symbolic_header;
  3615.       sym_offset = ecoff_data (abfd)->sym_filepos + sizeof (struct hdr_ext);
  3616.  
  3617. #define SET(offset, size, ptr) \
  3618.   if (symhdr->size == 0) \
  3619.     symhdr->offset = 0; \
  3620.   else \
  3621.     symhdr->offset = (((char *) ecoff_data (abfd)->ptr \
  3622.                - (char *) ecoff_data (abfd)->raw_syments) \
  3623.               + sym_offset);
  3624.  
  3625.       SET (cbLineOffset, cbLine, line);
  3626.       SET (cbDnOffset, idnMax, external_dnr);
  3627.       SET (cbPdOffset, ipdMax, external_pdr);
  3628.       SET (cbSymOffset, isymMax, external_sym);
  3629.       SET (cbOptOffset, ioptMax, external_opt);
  3630.       SET (cbAuxOffset, iauxMax, external_aux);
  3631.       SET (cbSsOffset, issMax, ss);
  3632.       SET (cbSsExtOffset, issExtMax, ssext);
  3633.       SET (cbFdOffset, ifdMax, external_fdr);
  3634.       SET (cbRfdOffset, crfd, external_rfd);
  3635.       SET (cbExtOffset, iextMax, external_ext);
  3636. #undef SET
  3637.  
  3638.       if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos,
  3639.             SEEK_SET) != 0)
  3640.     return false;
  3641.       ecoff_swap_hdr_out (abfd, &ecoff_data (abfd)->symbolic_header, &buff);
  3642.       if (bfd_write ((PTR) &buff, 1, sizeof buff, abfd) != sizeof buff)
  3643.     return false;
  3644.       if (bfd_write ((PTR) ecoff_data (abfd)->raw_syments, 1,
  3645.              ecoff_data (abfd)->raw_size, abfd)
  3646.       != ecoff_data (abfd)->raw_size)
  3647.     return false;
  3648.     }
  3649.  
  3650.   return true;
  3651. }
  3652.  
  3653. /* Archive handling.  ECOFF uses what appears to be a unique type of
  3654.    archive header (which I call an armap).  The byte ordering of the
  3655.    armap and the contents are encoded in the name of the armap itself.
  3656.    At least for now, we only support archives with the same byte
  3657.    ordering in the armap and the contents.
  3658.  
  3659.    The first four bytes in the armap are the number of symbol
  3660.    definitions.  This is always a power of two.
  3661.  
  3662.    This is followed by the symbol definitions.  Each symbol definition
  3663.    occupies 8 bytes.  The first four bytes are the offset from the
  3664.    start of the armap strings to the null-terminated string naming
  3665.    this symbol.  The second four bytes are the file offset to the
  3666.    archive member which defines this symbol.  If the second four bytes
  3667.    are 0, then this is not actually a symbol definition, and it should
  3668.    be ignored.
  3669.  
  3670.    The symbols are hashed into the armap with a closed hashing scheme.
  3671.    See the functions below for the details of the algorithm.
  3672.  
  3673.    We could use the hash table when looking up symbols in a library.
  3674.    This would require a new BFD target entry point to replace the
  3675.    bfd_get_next_mapent function used by the linker.
  3676.  
  3677.    After the symbol definitions comes four bytes holding the size of
  3678.    the string table, followed by the string table itself.  */
  3679.  
  3680. /* The name of an archive headers looks like this:
  3681.    __________E[BL]E[BL]_ (with a trailing space).
  3682.    The trailing space is changed to an X if the archive is changed to
  3683.    indicate that the armap is out of date.  */
  3684.  
  3685. #define ARMAP_BIG_ENDIAN 'B'
  3686. #define ARMAP_LITTLE_ENDIAN 'L'
  3687. #define ARMAP_MARKER 'E'
  3688. #define ARMAP_START "__________"
  3689. #define ARMAP_HEADER_MARKER_INDEX 10
  3690. #define ARMAP_HEADER_ENDIAN_INDEX 11
  3691. #define ARMAP_OBJECT_MARKER_INDEX 12
  3692. #define ARMAP_OBJECT_ENDIAN_INDEX 13
  3693. #define ARMAP_END_INDEX 14
  3694. #define ARMAP_END "_ "
  3695.  
  3696. /* This is a magic number used in the hashing algorithm.  */
  3697. #define ARMAP_HASH_MAGIC 0x9dd68ab5
  3698.  
  3699. /* This returns the hash value to use for a string.  It also sets
  3700.    *REHASH to the rehash adjustment if the first slot is taken.  SIZE
  3701.    is the number of entries in the hash table, and HLOG is the log
  3702.    base 2 of SIZE.  */
  3703.  
  3704. static unsigned int
  3705. ecoff_armap_hash (s, rehash, size, hlog)
  3706.      CONST char *s;
  3707.      unsigned int *rehash;
  3708.      unsigned int size;
  3709.      unsigned int hlog;
  3710. {
  3711.   unsigned int hash;
  3712.  
  3713.   hash = *s++;
  3714.   while (*s != '\0')
  3715.     hash = ((hash >> 27) | (hash << 5)) + *s++;
  3716.   hash *= ARMAP_HASH_MAGIC;
  3717.   *rehash = (hash & (size - 1)) | 1;
  3718.   return hash >> (32 - hlog);
  3719. }
  3720.  
  3721. /* Read in the armap.  */
  3722.  
  3723. static boolean
  3724. ecoff_slurp_armap (abfd)
  3725.      bfd *abfd;
  3726. {
  3727.   char nextname[17];
  3728.   unsigned int i;
  3729.   struct areltdata *mapdata;
  3730.   bfd_size_type parsed_size;
  3731.   char *raw_armap;
  3732.   struct artdata *ardata;
  3733.   unsigned int count;
  3734.   char *raw_ptr;
  3735.   struct symdef *symdef_ptr;
  3736.   char *stringbase;
  3737.   
  3738.   /* Get the name of the first element.  */
  3739.   i = bfd_read ((PTR) nextname, 1, 16, abfd);
  3740.   if (i == 0)
  3741.       return true;
  3742.   if (i != 16)
  3743.       return false;
  3744.  
  3745.   bfd_seek (abfd, (file_ptr) -16, SEEK_CUR);
  3746.  
  3747.   /* See if the first element is an armap.  */
  3748.   if (strncmp (nextname, ARMAP_START, sizeof ARMAP_START - 1) != 0
  3749.       || nextname[ARMAP_HEADER_MARKER_INDEX] != ARMAP_MARKER
  3750.       || (nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
  3751.       && nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
  3752.       || nextname[ARMAP_OBJECT_MARKER_INDEX] != ARMAP_MARKER
  3753.       || (nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
  3754.       && nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
  3755.       || strncmp (nextname + ARMAP_END_INDEX,
  3756.           ARMAP_END, sizeof ARMAP_END - 1) != 0)
  3757.     {
  3758.       bfd_has_map (abfd) = false;
  3759.       return true;
  3760.     }
  3761.  
  3762.   /* Make sure we have the right byte ordering.  */
  3763.   if (((nextname[ARMAP_HEADER_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
  3764.        ^ (abfd->xvec->header_byteorder_big_p != false))
  3765.       || ((nextname[ARMAP_OBJECT_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
  3766.       ^ (abfd->xvec->byteorder_big_p != false)))
  3767.     {
  3768.       bfd_error = wrong_format;
  3769.       return false;
  3770.     }
  3771.  
  3772.   /* Read in the armap.  */
  3773.   ardata = bfd_ardata (abfd);
  3774.   mapdata = snarf_ar_hdr (abfd);
  3775.   if (mapdata == (struct areltdata *) NULL)
  3776.     return false;
  3777.   parsed_size = mapdata->parsed_size;
  3778.   bfd_release (abfd, (PTR) mapdata);
  3779.     
  3780.   raw_armap = (char *) bfd_alloc (abfd, parsed_size);
  3781.   if (raw_armap == (char *) NULL)
  3782.     {
  3783.       bfd_error = no_memory;
  3784.       return false;
  3785.     }
  3786.     
  3787.   if (bfd_read ((PTR) raw_armap, 1, parsed_size, abfd) != parsed_size)
  3788.     {
  3789.       bfd_error = malformed_archive;
  3790.       bfd_release (abfd, (PTR) raw_armap);
  3791.       return false;
  3792.     }
  3793.     
  3794.   count = bfd_h_get_32 (abfd, (PTR) raw_armap);
  3795.  
  3796.   ardata->symdef_count = 0;
  3797.   ardata->cache = (struct ar_cache *) NULL;
  3798.  
  3799.   /* Hack: overlay the symdefs on top of the raw archive data.  This
  3800.      is the way do_slurp_bsd_armap works.  */
  3801.   raw_ptr = raw_armap + LONG_SIZE;
  3802.   symdef_ptr = (struct symdef *) raw_ptr;
  3803.   ardata->symdefs = (carsym *) symdef_ptr;
  3804.   stringbase = raw_ptr + count * (2 * LONG_SIZE) + LONG_SIZE;
  3805.  
  3806. #ifdef CHECK_ARMAP_HASH
  3807.   {
  3808.     unsigned int hlog;
  3809.  
  3810.     /* Double check that I have the hashing algorithm right by making
  3811.        sure that every symbol can be looked up successfully.  */
  3812.     hlog = 0;
  3813.     for (i = 1; i < count; i <<= 1)
  3814.       hlog++;
  3815.     BFD_ASSERT (i == count);
  3816.  
  3817.     for (i = 0; i < count; i++, raw_ptr += 2 * LONG_SIZE)
  3818.       {
  3819.     unsigned int name_offset, file_offset;
  3820.     unsigned int hash, rehash, srch;
  3821.       
  3822.     name_offset = bfd_h_get_32 (abfd, (PTR) raw_ptr);
  3823.     file_offset = bfd_h_get_32 (abfd, (PTR) (raw_ptr + LONG_SIZE));
  3824.     if (file_offset == 0)
  3825.       continue;
  3826.     hash = ecoff_armap_hash (stringbase + name_offset, &rehash, count,
  3827.                  hlog);
  3828.     if (hash == i)
  3829.       continue;
  3830.  
  3831.     /* See if we can rehash to this location.  */
  3832.     for (srch = (hash + rehash) & (count - 1);
  3833.          srch != hash && srch != i;
  3834.          srch = (srch + rehash) & (count - 1))
  3835.       BFD_ASSERT (bfd_h_get_32 (abfd,
  3836.                     (PTR) (raw_armap
  3837.                        + LONG_SIZE
  3838.                        + (srch * 2 * LONG_SIZE)
  3839.                        + LONG_SIZE))
  3840.               != 0);
  3841.     BFD_ASSERT (srch == i);
  3842.       }
  3843.   }
  3844.  
  3845.   raw_ptr = raw_armap + LONG_SIZE;
  3846. #endif /* CHECK_ARMAP_HASH */
  3847.  
  3848.   for (i = 0; i < count; i++, raw_ptr += 2 * LONG_SIZE)
  3849.     {
  3850.       unsigned int name_offset, file_offset;
  3851.  
  3852.       name_offset = bfd_h_get_32 (abfd, (PTR) raw_ptr);
  3853.       file_offset = bfd_h_get_32 (abfd, (PTR) (raw_ptr + LONG_SIZE));
  3854.       if (file_offset == 0)
  3855.     continue;
  3856.       symdef_ptr->s.name = stringbase + name_offset;
  3857.       symdef_ptr->file_offset = file_offset;
  3858.       ++symdef_ptr;
  3859.       ++ardata->symdef_count;
  3860.     }
  3861.  
  3862.   ardata->first_file_filepos = bfd_tell (abfd);
  3863.   /* Pad to an even boundary.  */
  3864.   ardata->first_file_filepos += ardata->first_file_filepos % 2;
  3865.  
  3866.   bfd_has_map (abfd) = true;
  3867.  
  3868.   return true;
  3869. }
  3870.  
  3871. /* Write out an armap.  */
  3872.  
  3873. static boolean
  3874. ecoff_write_armap (abfd, elength, map, orl_count, stridx)
  3875.      bfd *abfd;
  3876.      unsigned int elength;
  3877.      struct orl *map;
  3878.      unsigned int orl_count;
  3879.      int stridx;
  3880. {
  3881.   unsigned int hashsize, hashlog;
  3882.   unsigned int symdefsize;
  3883.   int padit;
  3884.   unsigned int stringsize;
  3885.   unsigned int mapsize;
  3886.   file_ptr firstreal;
  3887.   struct ar_hdr hdr;
  3888.   struct stat statbuf;
  3889.   unsigned int i;
  3890.   bfd_byte temp[LONG_SIZE];
  3891.   bfd_byte *hashtable;
  3892.   bfd *current;
  3893.   bfd *last_elt;
  3894.  
  3895.   /* Ultrix appears to use as a hash table size the least power of two
  3896.      greater than twice the number of entries.  */
  3897.   for (hashlog = 0; (1 << hashlog) <= 2 * orl_count; hashlog++)
  3898.     ;
  3899.   hashsize = 1 << hashlog;
  3900.  
  3901.   symdefsize = hashsize * 2 * LONG_SIZE;
  3902.   padit = stridx % 2;
  3903.   stringsize = stridx + padit;
  3904.  
  3905.   /* Include 8 bytes to store symdefsize and stringsize in output. */
  3906.   mapsize = LONG_SIZE + symdefsize + stringsize + LONG_SIZE;
  3907.  
  3908.   firstreal = SARMAG + sizeof (struct ar_hdr) + mapsize + elength;
  3909.  
  3910.   memset ((PTR) &hdr, 0, sizeof hdr);
  3911.  
  3912.   /* Work out the ECOFF armap name.  */
  3913.   strcpy (hdr.ar_name, ARMAP_START);
  3914.   hdr.ar_name[ARMAP_HEADER_MARKER_INDEX] = ARMAP_MARKER;
  3915.   hdr.ar_name[ARMAP_HEADER_ENDIAN_INDEX] =
  3916.     (abfd->xvec->header_byteorder_big_p
  3917.      ? ARMAP_BIG_ENDIAN
  3918.      : ARMAP_LITTLE_ENDIAN);
  3919.   hdr.ar_name[ARMAP_OBJECT_MARKER_INDEX] = ARMAP_MARKER;
  3920.   hdr.ar_name[ARMAP_OBJECT_ENDIAN_INDEX] =
  3921.     abfd->xvec->byteorder_big_p ? ARMAP_BIG_ENDIAN : ARMAP_LITTLE_ENDIAN;
  3922.   memcpy (hdr.ar_name + ARMAP_END_INDEX, ARMAP_END, sizeof ARMAP_END - 1);
  3923.  
  3924.   /* Write the timestamp of the archive header to be just a little bit
  3925.      later than the timestamp of the file, otherwise the linker will
  3926.      complain that the index is out of date.  Actually, the Ultrix
  3927.      linker just checks the archive name; the GNU linker may check the
  3928.      date.  */
  3929.   stat (abfd->filename, &statbuf);
  3930.   sprintf (hdr.ar_date, "%ld", (long) (statbuf.st_mtime + 60));
  3931.  
  3932.   /* The DECstation uses zeroes for the uid, gid and mode of the
  3933.      armap.  */
  3934.   hdr.ar_uid[0] = '0';
  3935.   hdr.ar_gid[0] = '0';
  3936.   hdr.ar_mode[0] = '0';
  3937.  
  3938.   sprintf (hdr.ar_size, "%-10d", (int) mapsize);
  3939.  
  3940.   hdr.ar_fmag[0] = '`';
  3941.   hdr.ar_fmag[1] = '\n';
  3942.  
  3943.   /* Turn all null bytes in the header into spaces.  */
  3944.   for (i = 0; i < sizeof (struct ar_hdr); i++)
  3945.    if (((char *)(&hdr))[i] == '\0')
  3946.      (((char *)(&hdr))[i]) = ' ';
  3947.  
  3948.   if (bfd_write ((PTR) &hdr, 1, sizeof (struct ar_hdr), abfd)
  3949.       != sizeof (struct ar_hdr))
  3950.     return false;
  3951.  
  3952.   bfd_h_put_32 (abfd, hashsize, temp);
  3953.   if (bfd_write (temp, 1, LONG_SIZE, abfd) != LONG_SIZE)
  3954.     return false;
  3955.   
  3956.   hashtable = (bfd_byte *) bfd_zalloc (abfd, symdefsize);
  3957.  
  3958.   current = abfd->archive_head;
  3959.   last_elt = current;
  3960.   for (i = 0; i < orl_count; i++)
  3961.     {
  3962.       unsigned int hash, rehash;
  3963.  
  3964.       /* Advance firstreal to the file position of this archive
  3965.      element.  */
  3966.       if (((bfd *) map[i].pos) != last_elt)
  3967.     {
  3968.       do
  3969.         {
  3970.           firstreal += arelt_size (current) + sizeof (struct ar_hdr);
  3971.           firstreal += firstreal % 2;
  3972.           current = current->next;
  3973.         }
  3974.       while (current != (bfd *) map[i].pos);
  3975.     }
  3976.  
  3977.       last_elt = current;
  3978.  
  3979.       hash = ecoff_armap_hash (*map[i].name, &rehash, hashsize, hashlog);
  3980.       if (bfd_h_get_32 (abfd, (PTR) (hashtable
  3981.                      + (hash * 2 * LONG_SIZE)
  3982.                      + LONG_SIZE))
  3983.       != 0)
  3984.     {
  3985.       unsigned int srch;
  3986.  
  3987.       /* The desired slot is already taken.  */
  3988.       for (srch = (hash + rehash) & (hashsize - 1);
  3989.            srch != hash;
  3990.            srch = (srch + rehash) & (hashsize - 1))
  3991.         if (bfd_h_get_32 (abfd, (PTR) (hashtable
  3992.                        + (srch * 2 * LONG_SIZE)
  3993.                        + LONG_SIZE))
  3994.         == 0)
  3995.           break;
  3996.  
  3997.       BFD_ASSERT (srch != hash);
  3998.  
  3999.       hash = srch;
  4000.     }
  4001.     
  4002.       bfd_h_put_32 (abfd, map[i].namidx,
  4003.             (PTR) (hashtable + hash * 2 * LONG_SIZE));
  4004.       bfd_h_put_32 (abfd, firstreal,
  4005.             (PTR) (hashtable + hash * 2 * LONG_SIZE + LONG_SIZE));
  4006.     }
  4007.  
  4008.   if (bfd_write (hashtable, 1, symdefsize, abfd) != symdefsize)
  4009.     return false;
  4010.  
  4011.   bfd_release (abfd, hashtable);
  4012.  
  4013.   /* Now write the strings.  */
  4014.   bfd_h_put_32 (abfd, stringsize, temp);
  4015.   if (bfd_write (temp, 1, LONG_SIZE, abfd) != LONG_SIZE)
  4016.     return false;
  4017.   for (i = 0; i < orl_count; i++)
  4018.     {
  4019.       bfd_size_type len;
  4020.  
  4021.       len = strlen (*map[i].name) + 1;
  4022.       if (bfd_write ((PTR) (*map[i].name), 1, len, abfd) != len)
  4023.     return false;
  4024.     }
  4025.  
  4026.   /* The spec sez this should be a newline.  But in order to be
  4027.      bug-compatible for DECstation ar we use a null.  */
  4028.   if (padit)
  4029.     {
  4030.       if (bfd_write ("\0", 1, 1, abfd) != 1)
  4031.     return false;
  4032.     }
  4033.  
  4034.   return true;
  4035. }
  4036.  
  4037. /* We just use the generic extended name support.  This is a GNU
  4038.    extension.  */
  4039. #define ecoff_slurp_extended_name_table    _bfd_slurp_extended_name_table
  4040.  
  4041. /* See whether this BFD is an archive.  If it is, read in the armap
  4042.    and the extended name table.  */
  4043.  
  4044. static bfd_target *
  4045. ecoff_archive_p (abfd)
  4046.      bfd *abfd;
  4047. {
  4048.   char armag[SARMAG + 1];
  4049.  
  4050.   if (bfd_read ((PTR) armag, 1, SARMAG, abfd) != SARMAG
  4051.       || strncmp (armag, ARMAG, SARMAG) != 0)
  4052.     {
  4053.       bfd_error = wrong_format;
  4054.       return (bfd_target *) NULL;
  4055.     }
  4056.  
  4057.   /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
  4058.      involves a cast, we can't do it as the left operand of
  4059.      assignment.  */
  4060.   abfd->tdata.aout_ar_data =
  4061.     (struct artdata *) bfd_zalloc (abfd, sizeof (struct artdata));
  4062.  
  4063.   if (bfd_ardata (abfd) == (struct artdata *) NULL)
  4064.     {
  4065.       bfd_error = no_memory;
  4066.       return (bfd_target *) NULL;
  4067.     }
  4068.  
  4069.   bfd_ardata (abfd)->first_file_filepos = SARMAG;
  4070.   
  4071.   if (ecoff_slurp_armap (abfd) == false
  4072.       || ecoff_slurp_extended_name_table (abfd) == false)
  4073.     {
  4074.       bfd_release (abfd, bfd_ardata (abfd));
  4075.       abfd->tdata.aout_ar_data = (struct artdata *) NULL;
  4076.       return (bfd_target *) NULL;
  4077.     }
  4078.   
  4079.   return abfd->xvec;
  4080. }
  4081.  
  4082. /* This is the COFF backend structure.  The backend_data field of the
  4083.    bfd_target structure is set to this.  The section reading code in
  4084.    coffgen.c uses this structure.  */
  4085.  
  4086. static CONST bfd_coff_backend_data bfd_ecoff_std_swap_table = {
  4087.   (void (*) PARAMS ((bfd *,PTR,int,int,PTR))) bfd_void, /* aux_in */
  4088.   (void (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* sym_in */
  4089.   (void (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* lineno_in */
  4090.   (unsigned (*) PARAMS ((bfd *,PTR,int,int,PTR))) bfd_void, /* aux_out */
  4091.   (unsigned (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* sym_out */
  4092.   (unsigned (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* lineno_out */
  4093.   ecoff_swap_reloc_out, ecoff_swap_filehdr_out, ecoff_swap_aouthdr_out,
  4094.   ecoff_swap_scnhdr_out,
  4095.   FILHSZ, AOUTSZ, SCNHSZ, 0, 0, 0, true,
  4096.   ecoff_swap_filehdr_in, ecoff_swap_aouthdr_in, ecoff_swap_scnhdr_in,
  4097.   ecoff_bad_format_hook, ecoff_set_arch_mach_hook, ecoff_mkobject_hook,
  4098.   ecoff_styp_to_sec_flags, ecoff_make_section_hook, ecoff_set_alignment_hook,
  4099.   ecoff_slurp_symbol_table
  4100. };
  4101.  
  4102. /* get_lineno could be written for ECOFF, but it would currently only
  4103.    be useful for linking ECOFF and COFF files together, which doesn't
  4104.    seem too likely.  */
  4105. #define ecoff_get_lineno \
  4106.   ((alent *(*) PARAMS ((bfd *, asymbol *))) bfd_nullvoidptr)
  4107.  
  4108. /* These bfd_target functions are defined in other files.  */
  4109.  
  4110. #define ecoff_core_file_failing_command    _bfd_dummy_core_file_failing_command
  4111. #define ecoff_core_file_failing_signal    _bfd_dummy_core_file_failing_signal
  4112. #define ecoff_core_file_matches_executable_p \
  4113.   _bfd_dummy_core_file_matches_executable_p
  4114. #define ecoff_truncate_arname        bfd_dont_truncate_arname
  4115. #define ecoff_openr_next_archived_file    bfd_generic_openr_next_archived_file
  4116. #define ecoff_generic_stat_arch_elt    bfd_generic_stat_arch_elt
  4117. #define    ecoff_get_section_contents    bfd_generic_get_section_contents
  4118. #define ecoff_get_reloc_upper_bound    coff_get_reloc_upper_bound
  4119. #define    ecoff_close_and_cleanup        bfd_generic_close_and_cleanup
  4120. #define ecoff_bfd_debug_info_start    bfd_void
  4121. #define ecoff_bfd_debug_info_end    bfd_void
  4122. #define ecoff_bfd_debug_info_accumulate    \
  4123.   ((void (*) PARAMS ((bfd *, struct sec *))) bfd_void)
  4124. #define ecoff_bfd_get_relocated_section_contents \
  4125.   bfd_generic_get_relocated_section_contents
  4126. #define ecoff_bfd_relax_section        bfd_generic_relax_section
  4127. #define ecoff_bfd_make_debug_symbol \
  4128.   ((asymbol *(*) PARAMS ((bfd *, void *, unsigned long))) bfd_nullvoidptr)
  4129.  
  4130. bfd_target ecoffalpha_little_vec =
  4131. {
  4132.   "ecoff-littlealpha",        /* name */
  4133.   bfd_target_ecoff_flavour,
  4134.   false,            /* data byte order is little */
  4135.   false,            /* header byte order is little */
  4136.  
  4137.   (HAS_RELOC | EXEC_P |        /* object flags */
  4138.    HAS_LINENO | HAS_DEBUG |
  4139.    HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT),
  4140.  
  4141.   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* sect
  4142.                                 flags */
  4143.   0,                /* leading underscore */
  4144.   '/',                /* ar_pad_char */
  4145.   15,                /* ar_max_namelen */
  4146.   3,                /* minimum alignment power */
  4147.   _do_getl64, _do_getl_signed_64, _do_putl64,
  4148.      _do_getl32, _do_getl_signed_32, _do_putl32,
  4149.      _do_getl16, _do_getl_signed_16, _do_putl16, /* data */
  4150.   _do_getl64, _do_getl_signed_64, _do_putl64,
  4151.      _do_getl32, _do_getl_signed_32, _do_putl32,
  4152.      _do_getl16, _do_getl_signed_16, _do_putl16, /* hdrs */
  4153.  
  4154.   {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
  4155.      ecoff_archive_p, _bfd_dummy_target},
  4156.   {bfd_false, ecoff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
  4157.      bfd_false},
  4158.   {bfd_false, ecoff_write_object_contents, /* bfd_write_contents */
  4159.      _bfd_write_archive_contents, bfd_false},
  4160.   JUMP_TABLE (ecoff),
  4161.   (PTR) &bfd_ecoff_std_swap_table
  4162. };
  4163.  
  4164.